【问题标题】:Loading more than 10 images on iPhone?在 iPhone 上加载超过 10 张图像?
【发布时间】:2011-02-27 06:33:31
【问题描述】:

我正在尝试在 ScrollView 上添加 10 多张图片。

NSUInteger i;
for (i = 1; i <= numberOfImage; i++)
{
    NSString *imageName = [NSString stringWithFormat:@"d%dimage%d.png", imageSection, i];               

    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    CGRect rect = imageView.frame;
    rect.size.height = kScrollObjHeight;
    rect.size.width = kScrollObjWidth;
    imageView.frame = rect;
    imageView.tag = i;  // tag our images for later use when we place them in serial fashion
    [scrollView addSubview:imageView];
    [imageView release];
}

此代码来自 Apple 示例,它运行良好。但如果变量 'i' 大于 10,则 'UIImage *image' 为空。 imageName 似乎是正确的。但我不知道为什么它不加载图像。有人看出问题了吗?

还有一件事。如果我这样做,iOS 会自动控制内存吗?我的意思是,如果所有(超过 10 个)图像都加载到内存中,即使它们没有显示,这也是一种浪费内存。我听说 iOS 会加载仅显示在屏幕上的图像和未显示的免费图像。是对的吗?

感谢阅读。

【问题讨论】:

  • 尝试静态设置框架,看看屏幕上是否出现图像

标签: iphone image uiscrollview


【解决方案1】:

UIimage imageNamed:缓存文件内容。我建议你使用 UIImage +imageWithContentsOfFile: 在这种情况下根本不缓存。

【讨论】:

  • +1 对于 Kazuki .....对于这个人的正确指导.....是的,它会缓存图像(UIimage imageNamed :),如果您使用更多图像,您通常会得到内存警告使用 this.So SeniorLee 使用方法 imageWithContentsOfFile:
【解决方案2】:

您必须确保图像具有正确的名称(如 0dimage11.jpg)并添加到 XCode 项目中。

【讨论】:

  • 我做到了。检查文件名,所有这些都添加到项目中。
【解决方案3】:

您可能必须相应地设置 contentSize。

除非您使用基于 CATiledLayer 的 UIView,否则 IOS 不会做这种神奇的内存管理。

如果 UIImage 没有创建,那是因为名称没有引用资源文件夹中的图像,你应该有一个例外。

【讨论】:

  • contentSize 对创建图像有影响吗?我提到没有创建'UIImage *image'。这与“现在显示已创建的图像”无关。
【解决方案4】:

您需要有正确的文件名。你说你认为文件名是正确的。

NSLog(@"Loop %d: d%dimage%d.png", i,imageSection, i]);

注销文件名,以便查看名称的实际含义。将该行放在循环中。

NSUInteger i;
for (i = 1; i <= numberOfImage; i++)
{
    NSString *imageName = [NSString stringWithFormat:@"d%dimage%d.png", imageSection, i];               
    NSLog(@"Loop %d: d%dimage%d.png", i,imageSection, i]);
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    CGRect rect = imageView.frame;
    rect.size.height = kScrollObjHeight;
    rect.size.width = kScrollObjWidth;
    imageView.frame = rect;
    imageView.tag = i;  // tag our images for later use when we place them in serial fashion
    [scrollView addSubview:imageView];
    [imageView release];
}

然后在调试器中监控文件名并查看这些图像文件是否存在,以及在 Apple 放置它们的图像文件的同一目录中。

【讨论】:

  • 好吧,我想我做得还不错。文件名就像 d2image10.png, d2image11.png, d2image12.... 所有这些都在 xcode 项目中。可能是什么问题??
【解决方案5】:

嘿,
一次加载 10 张图像不是很好的方法。所有你做的事情都是正确的,但你的图像仍然显示为空,然后请检查日志可能有内存警告。你可以使用苹果的示例代码照片卷轴。它会做你想做的所有事情,还可以管理良好的内存。有两种方法,一种是使用 CATieldLayer,另一种是直接加载图像。我建议您使用使用 CATieldLayer 的方法。

【讨论】:

  • 谢谢。我没有看到任何关于内存的日志,但我猜这也与内存不足有关。我没有时间研究照片卷轴。因为我参加了一个关于只制作一天应用程序的节日。它被称为 AppJam。不管怎样,谢谢。我会试试照片卷轴。
【解决方案6】:

对不起各位。事实证明这是我的错。好吧,不完全是我的错。设计师向我抛出了许多文件名错误的文件,例如 d1imgae11.png……无论如何,你们所有人的提示给了我不同的观点来看待问题,我得到了另一个关于不缓存图像的提示。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多