【问题标题】:Downloading lots of large images then displaying in small UIImageViews in iPhone app. Do I need to resize to conserve memory?下载大量大图像,然后在 iPhone 应用程序的小 UIImageViews 中显示。我需要调整大小以节省内存吗?
【发布时间】:2011-07-14 11:41:49
【问题描述】:

我正在开发一个应用程序,该应用程序从网络服务器下载一系列图像并将它们显示在应用程序中。来自服务器的图像大约为 615x725,但在应用程序中仅显示为大约 200x230。我的应用程序中的内存使用率现在非常高,并且我经常收到内存警告。它们是通过这组调用下载的:

向 NSData 同步 ASIHTTPRequest(在 GCD 后台线程中)

[UIImage imageWithData:...]

[image forceLoad] (see below)

[UIImageView initWithImage:...]

[imageview setFrame:...]

我还需要做更多的事情来确保我的内存不会被不必要的大 UIImage 数据占用吗?或者 UIImageView 会为我处理这个吗?

强制加载方法:

- (void) forceLoad{
    const CGImageRef cgImage = [self CGImage];  

    const int width = CGImageGetWidth(cgImage);
    const int height = CGImageGetHeight(cgImage);

    const CGColorSpaceRef colorspace = CGImageGetColorSpace(cgImage);
    const CGContextRef context = CGBitmapContextCreate(
                                                   NULL, /* Where to store the data. NULL = don’t care */
                                                   width, height, /* width & height */
                                                   8, width * 4, /* bits per component, bytes per row */
                                                   colorspace, kCGImageAlphaNoneSkipFirst);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
    CGContextRelease(context);

}

【问题讨论】:

  • 没有办法让服务器为你的应用交付更小的版本吗?这可能会提高整体性能并解决您的大小调整问题。
  • 嗨尼克,对不起,我忘了提到可能可以从服务器获取较小的图像,但我暂时不知道(我与服务器管理员完全断开了连接)所以我想将此信息作为备份以备不时之需,以备将来在其他应用中参考。
  • 如果您能获得较小的图像,那就去做吧!对于普通网站来说,这甚至是一个很好的建议,不要让客户端(浏览器)处理大小调整,因为这会导致客户端需要更高的带宽和 cpu 时间:用户体验可能会受到影响。所以在像 iphone 这样的低带宽环境中,这更适用;)
  • 回到您的内存使用问题:您是否使用性能工具运行您的应用程序?它可以轻松地向您展示最消耗您资源的内容。
  • 是的,我完全理解在通过网络传输之前从源头调整图像大小的各种好处,但正如我所提到的,它可能超出我的控制范围。 :)

标签: iphone memory-management uiimageview uiimage


【解决方案1】:

您可以按如下方式调整大小和图像,希望对您有所帮助

UIImage *newImage;
UIImage *olDImage = [UIImage imageWithData:imageData] ;
UIGraphicsBeginImageContext(CGSizeMake(200,230)); 
[olDImage drawInRect:CGRectMake(0, 0,200,230)];
newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();

【讨论】:

  • 谢谢。这实际上与将其传递给更小尺寸的 UIImageVew 有什么不同吗?
猜你喜欢
  • 2011-10-09
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
相关资源
最近更新 更多