iOS-UIImage imageWithContentsOfFile 和 imageName 对照
1.imageWithContentsOfFile
NSString *imagePath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],@"icon.png"];
UIImage *imageI = [UIImage imageWithContentsOfFile:imagePath];
imageWithContentsOfFile的方式,在使用完毕之后系统会释放。不会缓存下来,所以也就没有这样的问题。一般也不会把全部的图片都会缓存。有些图片在应用中仅仅使用一两次的,就能够用这样的方式。比方新手引导界面的图片等等,就适合这样的方式。
没有明显的界限。
2.imageName
UIImage *image = [UIImage imageNamed:@"icon"];
imageName的方式会在使用的时候系统会cache,程序猿是无法处理cache,这是由系统自己主动处理的。对于反复载入的图像,速度会提升非常多,这样反而用户体验好。所以假设某张图片须要在应用中使用多次,或者反复引用。使用imageName的方式会更好,
所以,在app中一些常常会使用的,须要反复载入的,使用imageName会提升用户体验!
Apple的官方文档:
imageNamed: 这种方法用一个指定的名字在系统缓存中查找并返回一个图片对象假设它存在的话。假设缓存中没有找到对应的图片,这种方法从指定的文档中载入然后缓存并返回这个对象。
因此imageNamed的长处是当载入时会缓存图片。所以当图片会频繁的使用时,那么用imageNamed的方法会比較好。比如:你须要在 一个TableView里的TableViewCell里都载入相同一个图标,那么用imageNamed载入图像效率非常高。
系统会把那个图标Cache到内存,在TableViewCell里每次利用那个图 像的时候,仅仅会把图片指针指向同一块内存。正是因此使用imageNamed会缓存图片。即将图片的数据放在内存中,iOS的内存非常珍贵并且在内存消耗过大时。会强制释放内存,即会遇到memory warnings。而在iOS系统里面释放图像的内存是一件比較麻烦的事情。有可能会造成内存泄漏。比如:当一 个UIView对象的animationImages是一个装有UIImage对象动态数组NSMutableArray,并进行逐帧动画。当使用imageNamed的方式载入图像到一个动态数组NSMutableArray,这将会非常有可能造成内存泄露。
原因非常显然的。
imageWithContentsOfFile:仅载入图片。图像数据不会缓存。因此对于较大的图片以及使用情况较少时,那就能够用该方法,降低内存消耗。
一、载入图片问题
UIImage image = [UIImage imageNamed:imageFileName];
这样的图片载入方式带有图片缓存的功能。使用这样的方式载入图片后。图片会自己主动添加系统缓存中。并不会马上释放到内存。一些资源使程序中常常使用的图片资源。
使用这样的方式会加快程序的执行降低IO操作,但对于项目中仅仅用到一次的图片。假设採用这样的方案载入。会增导致程序的内存使用添加。
下面为官方对此方法的解释说明:
imageNamed:
Returns the image object associated with the specified filename.
- (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
in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.
二、非缓存的载入方式
+ (UIImage )imageWithContentsOfFile:(NSString )path
- (UIImage )imageWithData:(NSData )data
三、何时使用imageNamed方法
1、採用imageNamed方法的图片载入情况
图片资源反复使用到,如button背景图片的蓝色背景。这些图片要常常常使用到,并且占用内存少
2、不应该採用的情况:
(1)图片一般仅仅使用一次。如一些用户的照片资源
(2)图片资源较大,载入到内存后,比較耗费内存资源
iOS-UIImage imageWithContentsOfFile 和 imageName 对照的更多相关文章
- iOS UIImage DownLoad图片的下载缓存全部在此
iOS图片的下载缓存全部在此 分类: iOS编程 -- : 2075人阅读 评论() 收藏 举报 注意: 我的文章只写给自己看 ------------------------------------ ...
- UIImage imageNamed和UIImage imageWithContentsOfFile区别
UIImage imageNamed和 [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageNam ...
- 【原】[UIImage imageWithContentsOfFile:]引发的图片无法显示的问题
最近在做一个iOS手机项目的时候,遇到一个奇怪的问题,这里跟大家分享一下. 一.问题重现 1.启动App后,通过http请求下载了一个1.jpg文件到Cache目录下,下载成功之后,将图片显示在界面上 ...
- 第3月30天 UIImage imageWithContentsOfFile卡顿 Can't add self as subview MPMoviePlayerControlle rcrash
1. UIImage imageWithContentsOfFile卡顿 [[UIImage alloc] initWithContentsOfFile 卡顿 2.uitableview scroll ...
- [UIImage imageWithContentsOfFile:@""] 内存警告
You will want to use the [UIImage imageWithContentsOfFile:@""] method, as that doesn't cac ...
- iOS UIImage:获取图片主色调
本文转载至 http://www.wahenzan.com/a/mdev/ios/2015/0325/1677.html -(UIColor*)mostColor{ #if __IPHONE_OS_V ...
- [iOS] UIImage和CGImageRef
CGImageRef并不是面向对象的API,也不是类,只是一个指针类型,Quartz 2D对CGImageRef的定义为: typedef struct CGImage *CGImageRef; 由此 ...
- iOS UIimage初始化时的两种方法
第一种方式:UIImage *image = [UIImage imageNamed:@"image"]; 使用这种方式,第一次读取的时候,先把这个图片存到缓存里,下次再使用时直接 ...
- iOS UIImage扩展方法(category):放大、旋转、合并UIImage、增加渐变层、添加阴影、调节透明度、保存到相册
一有用的 UIImage 扩展,支持(等比例)放大和旋转可在许多 App 中使用. UIImage-Extensions.h #import <Foundation/Foundation.h&g ...
随机推荐
- Android ViewGroup拦截触摸事件具体解释
前言 在自己定义ViewGroup中.有时候须要实现触摸事件拦截.比方ListView下拉刷新就是典型的触摸事件拦截的样例. 触摸事件拦截就是在触摸事件被parent view拦截,而不会分发给其ch ...
- POJ--2112--Optimal Milking【Floyd+Dinic+二分答案】
链接:http://poj.org/problem?id=2112 题意:有k个挤奶器.编号1~k,c头牛,编号k+1~k+c,每一个挤奶器最多能给m头牛挤奶,给你一个k+c的邻接矩阵.要求每头牛都能 ...
- linux:共享内存
#include <sys/ipc.h> #include <sys/shm.h> #include <string.h> #include <stdio.h ...
- 使用Handler在子线程中更新UI
Android规定仅仅能在主线程中更新UI.假设在子线程中更新UI 的话会提演示样例如以下错误:Only the original thread that created a view hierach ...
- WPF Prefix 'attach' does not map to a namespace.
这个是用附加属性时,一定要在属性前面加Path= Visibility="{Binding Path=PlacementTarget.(attach:CommonAttachedProper ...
- SGU 531 - Bonnie and Clyde 预处理+二分
Bonnie and Clyde Description Bonnie and Clyde are into robbing banks. This time their target is a to ...
- ASP.NET Core-组件:目录
ylbtech-ASP.NET Core-组件:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http ...
- windows安装gnvm安装教程,node多版本解决方案
本文是实现windows下node多版本管理 Win10专业版 一.安装前准备 安装前请卸载node相关的所有东西!!! 二.gnvm下载 gnvm搜索 http://ksria.com/gnvm/ ...
- 关于flex布局中的兼容性问题
这几天在做项目中用到了flex布局,但是在测试的过程中发现他的兼容性实在是太差了,仅仅用到水平和垂直居中的样式,没想到兼容性代码就写了好几行. display:flex; display:-webki ...
- Java面向对象的概念整理
Java中一切皆是对象! 注意:8种基本数据类型不支持面向对象的编程机制,基本数据类型的数据也不具备“对象”的特性:没有成员变量.方法可以被调用. 为解决8种基本数据类型不能当成Object类型(所有 ...