【问题标题】:Loading Images from an array to the screen on iPhone [closed]将图像从数组加载到 iPhone 上的屏幕 [关闭]
【发布时间】:2013-03-23 00:46:43
【问题描述】:

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

}

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

【问题讨论】:

  • imagePaths 等于nil
  • 图片肯定是 .jpg 而不是 .png/.jpeg 格式? IMO 也可能导致问题。

标签: iphone xcode arrays debugging


【解决方案1】:

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

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

它将为零。这意味着您的 imagePaths 数组将是 null

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

【讨论】:

  • 我尝试在 for 循环上方添加那行代码,但它给了我 2 个错误。 Apple Mach-o Linker Error "_NSlog",引用自:-[ViewController viewDidLoad] in ViewController.o 和 Apple Mach-O Linker Error, Linker command failed with exit code 1 (use -v to see invocation) 有什么想法吗?跨度>
  • 是的。使用 NSLog(@"imagePaths count is: %d",[imagePaths count]);
  • 代码现在可以正常工作,但计数为 0
  • 是的..那个原因,,,,
【解决方案2】:
// 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];
}

【讨论】:

  • 你能解释一个完整的菜鸟吗?哈哈
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2021-10-10
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多