【问题标题】:Crash issue while picking a large size image from the iphone library in iPhone 3G从 iPhone 3G 中的 iphone 库中选择大尺寸图像时出现崩溃问题
【发布时间】:2011-05-17 17:48:19
【问题描述】:

我在从 iphone 3G 的照片库中获取大图像 (3.5 MB) 时遇到崩溃问题(这是由于内存不足)。问题仅出在 iphone 3G 上,而它在 iPhone 3GS 和 iPhone 4 上运行良好。我正在将这张图片从我的电子邮件中保存到我的 iphone 库中,并且似乎保存到库中的图像没有得到优化。这是一个错误吗?任何人都可以建议我一个解决方案。可以限制用户从 iphone 3G 的库中选择非常大的图像。我尝试使用苹果提供的示例应用程序来获取这个大图像,即使这样它也会崩溃。

【问题讨论】:

  • 我认为您无法限制用户选择大图像。但我也认为,崩溃不是因为他们选择了大图像。与 iPhone 内存相比,3.5MB 不算大
  • 3.5MB 与 iPhone GPU 的纹理内存相比可能太大了,这是播放大图像时的关键资源。已知 2G 和 3G 在纹理大于 1k x 1k 时存在问题。 3GS 和 4 具有更新的 GPU。

标签: iphone


【解决方案1】:

是在您选择图像时发生崩溃,还是在您尝试将图像放入 UIImageView 时发生?如果是后者,您可能需要先将图像缩放到较小的分辨率,然后再将其放入 UIImageView。我已经看到建议在 UIImageView 中应避免使用大于 1024 x 1024 的图像尺寸。

您可以使用如下代码执行此操作:

if (image.size.width > 1024 || image.size.height > 1024) {
    // resize the image
    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = self.frame.size.width/self.frame.size.height;

    if(imgRatio < maxRatio){
        imgRatio = self.frame.size.height / actualHeight;
        actualWidth = imgRatio * actualWidth;
        actualHeight = self.frame.size.height;
    } else {
        imgRatio = self.frame.size.width / actualWidth;
        actualHeight = imgRatio * actualHeight;
        actualWidth = self.frame.size.width;
    }
    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    imageToDraw = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

请注意,imageToDraw 是在此代码之外声明的 UIImage *,并且是可以放入 UIImageView 的最终图像。这段代码来自我编写的自定义视图中的一个方法,因此您在代码中看到 self.frame 的地方,您可以替换为您将图像缩放到的任何帧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多