2013-03-23 21 views
1

Im新的Xcode和im試圖從數組中拉出一堆圖片,並把它們放在屏幕上,但通過調試器,我看到我的for循環沒有運行。從陣列加載圖像到iPhone上的屏幕

for (int i = 0; i <[imagePaths count]; i++) 
{ 
    NSLog(@"I'm loading a card"); 

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 250, 350)]; 
    NSString *imgFilepath = [[NSBundle mainBundle] pathForResource:[imagePaths objectAtIndex:i] ofType:@"jpg"]; 
    UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgFilepath]; 
    [imgView setImage:img]; 
    [self.view addSubview:imgView]; 

} 

有沒有人看到我缺少什麼使這項工作?

+0

是'imagePaths'等於'nil'? – 2013-03-23 00:50:31

+0

圖片的確格.jpg不是.png/.jpeg格式? IMO也可能導致問題。 – 2013-03-23 03:54:20

+0

是的,他們是.jpg – DarkAssassin 2013-03-26 01:35:09

回答

1

我看到我的for循環沒有運行。

檢查您的[imagePaths count]使用下面的代碼在循環之前。

它將爲零。這意味着您的imagePaths陣列將null

NSlog(@"imagePaths count is: %d",[imagePaths count]); 
+0

我試着在for循環之上添加那行代碼,但它給了我兩個錯誤。蘋果Mach-o鏈接器錯誤「_NSlog」,引用自:ViewController.o中的[ViewController viewDidLoad]和Apple Mach-O鏈接器錯誤,鏈接器命令失敗,退出代碼1(使用-v查看調用)有什麼想法? – DarkAssassin 2013-03-26 00:57:15

+0

是的。使用NSLog(@「imagePaths count is:%d」,[imagePaths count]); – 2013-03-26 01:47:36

+0

該代碼現在工作沒有錯誤,但計數爲0 – DarkAssassin 2013-03-28 01:10:15

0
// Check what your imagePaths array contains, if it contains full path of image including filename and extension then use following code 
for (int i = 0; i <[imagePaths count]; i++) 
{ 
    NSLog(@"I'm loading a card"); 

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 250, 350)]; 
    UIImage *img = [[UIImage alloc] initWithContentsOfFile:[imagePaths objectAtIndex:i]]; 
    [imgView setImage:img]; 
    [self.view addSubview:imgView]; 
} 
+0

你可以解釋一個完整的noob?哈哈 – DarkAssassin 2013-03-26 01:32:42

0

如果它不運行,則必須imagePaths是nil。在它之前添加一個斷點並檢查是否有任何內容。

可能的新手錯誤:您是否正確分配並初始化了imagePaths?