1

我是 Xcode 的新手,我试图从数组中提取一堆图片并将它们放在屏幕上,但是通过调试器我看到我的 for 循环没有运行。

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];

}

有谁看到我缺少什么来完成这项工作?

4

3 回答 3

1

我看到我的 for 循环没有运行。

在循环之前检查您[imagePaths count]使用以下代码。

它将为零。这意味着您的imagePaths阵列将是null

NSlog(@"imagePaths count is: %d",[imagePaths count]);
于 2013-03-23T01:14:57.607 回答
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];
}
于 2013-03-23T08:14:16.847 回答
0

如果它没有运行,那么 imagePaths 必须为零。在它之前添加一个断点并检查它是否有任何东西。

可能的新手错误:您是否正确分配和初始化 imagePaths?

于 2013-03-23T19:49:18.560 回答