何时缓存UIImage资源是否值得? [英] When is it worthwhile to cache UIImage resources?

查看:84
本文介绍了何时缓存UIImage资源是否值得?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用图像文件进行大量的iPhone UI工作,这些图像文件在单个视图中的多个位置或整个应用程序的多个视图中使用。在某些情况下,我正在绘制新的图标,通常是通过合成2个小图像(每个小于4 KB)。

I've been doing a lot of iPhone UI work with image files that are used in multiple locations in a single view or in several views throughout the application. In some cases, I'm drawing new icons, usually by compositing 2 small images (each less than 4 KB).

我想了一下优化加载图像,但我不确定最佳做法是什么。我猜想保存使用CG函数创建或更改的任何图像是值得的。对于未更改的图像,从捆绑中加载图像的开销是多少?

I've thought a bit about optimizing the loading of images, but I'm not sure what the best practices would be. I would guess that it would be worthwhile to save any images that are created or altered using CG functions. With images that aren't altered, what is the overhead of loading images from a bundle?

UIImage* image = [UIImage imageNamed:@"myImage.png"]

考虑到移动设备的内存限制,有哪些因素在考虑缓存图像时最重要的是什么?我想到了图像的大小,可以缓存的图像总数以及加载单个图像的次数。

With the memory constraints of a mobile device in mind, what factors are most important when considering caching images? The size of the image, the total number of images that may be cached, and the number of times a single image is loaded come to mind.

推荐答案

在WWDC(2011)的最新演出会议中,Apple建议不要在大多数情况下缓存图像。他们建议您只有在知道事实后才能缓存图像,在性能分析之后,您需要提前缓存图像,因为您无法花时间从磁盘加载它们并对其进行解码。在大多数情况下,你可能负担得起。

In the latest performance sessions at WWDC (2011), Apple didn't recommend caching images for most cases. They recommend that you only cache images when you know for a fact, after a performance analysis, that you need to cache images ahead of time because you can't afford the time to load them off disk and decode them. In most cases you probably can afford it.

他们特别指出,就像@Till那样, + [UIImage imageNamed:] 缓存进程生命周期中的图像,因此他们建议使用非缓存加载方法,例如 + [UIImage imageWithContentsOfFile:]

They specifically noted, as @Till does, that +[UIImage imageNamed:] caches images for the lifetime of your process, and so they recommend using a non-caching loading method, such as +[UIImage imageWithContentsOfFile:]

原因是内存是iOS设备上受限制的资源,所以如果你缓存你的图像,你可能会对系统造成内存压力,并且应用程序会被jetsammed。而且,由于iOS 5首先使用更多内存,因此如果您正在缓存一堆UIImages,那么您的应用程序将更有可能获得jetsammed。

The reason is that memory is a constrained resource on iOS devices, so if you cache your images, you are likely to cause memory pressure on the system, and apps to get jetsammed. And since iOS 5 jetsams apps using more memory first, if you're caching a bunch of UIImages you're going to make it more likely for your app to get jetsammed.

这篇关于何时缓存UIImage资源是否值得?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆