图片显示 imageNamed, initWithContentsOfFile 的区别

来源:互联网 发布:手机照片整理软件 编辑:程序博客网 时间:2024/05/16 12:47

1.用imageNamed方法

[UIImage imageNamed:ImageName];

此方法为常见方法,利用它可以方便加载资源图片。用imageNamed的方式加载时,会把图像数据根据它的名字缓存在系统内存中,以提高imageNamed方法获得相同图片的image对象的性能。即使生成的对象被 autoReleasePool释放了,这份缓存也不释放。而且没有明确的释放方法。如果图像比较大,或者图像比较多,用这种方式会消耗很大的内存。

2.用 imageWithContentsOfFile 方法

NSString *thumbnailFile = [NSString stringWithFormat:@"%@/%@.png", [[NSBundle mainBundle] resourcePath], fileName];UIImage *thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile];

此方法加载的图片是不会缓存的。得到的对象时autoRelease的,当autoReleasePool释放时才释放。

3. 用initWithContentsFile方法

UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath]

此方法要手动release掉。不系统缓存。release后立即释放,一般用在封面等图比较大的地方。

0 0
原创粉丝点击