ios开发UIImage imageNamed方法的误用

1、加载图片问题

UIImage image = [UIImage imageNamed:imageFileName]; api

这种图片加载方式带有图片缓存的功能,使用这种方式加载图片后,图片会自动加入系统缓存中,并不会当即释放到内存。一些资源使程序中常常使用的图片资源, 缓存

使用这种方式会加快程序的运行减小IO操做,但对于项目中只用到一次的图片,若是采用这种方案加载,会增致使程序的内存使用增长。 app

如下为官方对此方法的解释说明: this

imageNamed:

Returns the image object associated with the specified filename. spa

+ (UIImage *)imageNamed:( NSString *) name
Parameters
name

The name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle. 图片

Return Value

The image object for the specified file, or nil if the method could not find the specified image. 内存

Discussion

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already ci

in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object. 资源


2、非缓存的加载方式

+ (UIImage *)imageWithContentsOfFile:(NSString *)path it

+ (UIImage *)imageWithData:(NSData *)data


3、什么时候使用imageNamed方法


一、采用imageNamed方法的图片加载状况


图片资源反复使用到,如按钮背景图片的蓝色背景,这些图片要常常用到,并且占用内存少


二、不该该采用的状况:


(1)图片通常只使用一次,如一些用户的照片资源

(2)图片资源较大,加载到内存后,比较耗费内存资源

相关文章
相关标签/搜索