多个ImageView 加载高清大图的内存管理
2015-11-11 19:07
435 查看
当我们在某一个View 多个UIImageView,且UIImageView都显示的是高清大图,就有可能出现内存警告的问题。如果第一次进入这个view,没有发生内存警告,当再次进入这个view,如果上一次的内存没有及时释放,这一次次的累加,便可导致内存崩溃。
1,UIImage 加载图片的方式。
如果是本地图片,尽量不要使用 [UIImage imageNamed:nil]; 这种方式,如果使用这种方式加载,只要程序不退出,它便一直会在内存中。
我们可以使用 :
NSString *path = [[NSBundlemainBundle]pathForResource:@'"图片的名字" ofType:@""];
UIImage *image = [UIImageimageWithContentsOfFile:path];
那两者的优缺点就很明显了,[UIImage imageNamed:nil]; 只需加载一次,它便在内存中,所以第二次加载速度很快。而第二种加载方式由于我们将它释放掉了,会再次加载。所以选用那种方式,依你情况而定。
2,上面说的第二种方式,虽然可以释放掉,但我们要告诉人家什么时候释放。也就是说,当前显示页面不是这个view时,我们便将它释放掉:
- (void)viewWillDisappear:(BOOL)animated{
[UIImageView removeFromSuperview];
UiImageView = nil;
}
当然,当我们再次进入这个view时,便要将移除掉的view再次添加进来
- (void)viewDidAppear:(BOOL)animated{
[self addSubView:UIImageView];
}
3,上述两种方式,主要解决内存累加的问题。但如果第一次进入view,图片全部渲染在view上时,内存就崩溃了。那我们只能在图片上做文章了。我们加载的高清大图如果差不多都是3000*2000,也可能比这个还大,就算我们的程序是iPad App,iPad 4 的分辨率才多少,这些图远远大于设备的分辨率,完全是资源浪费,所以我们通常的一个做法,便是将这样的图以小尺寸渲染到view上。
推荐使用:
UIImage+Resize.h, UIImage+Resize.mExtends the UIImage class to support resizing (optionally preserving the original aspect ratio), cropping, and generating thumbnails.
UIImage+RoundedCorner.h, UIImage+RoundedCorner.mExtends the UIImage class tosupport adding
rounded corners to an image.
UIImage+Alpha.h, UIImage+Alpha.mExtends the UIImage class with helper methods for working with alpha layers
(transparencies).
常用方法:
UIImage *image
UIImage *thumbImage = [imagethumbnailImage:140// This should the size of the view in collection view. example: myCell width is 20 and height is 20.
transparentBorder:0
cornerRadius:0
interpolationQuality:kCGInterpolationMedium]; //生成缩略图
// this "resizedimage" image is what you want to pass to setImage
UIImage * resizedImage = [imageresizedImage:imageview.frame.sizeinterpolationQuality: kCGInterpolationLow]; //生成你想要尺寸的图
造成的问题,要注意缩放的比例,不要导致图片变形,由于尺寸缩小,可能会导致图片模糊,注意缩小的尺寸。
综上可见,每种方法有优点,有缺点。主要依据自己的开发情况,折中使用。
1,UIImage 加载图片的方式。
如果是本地图片,尽量不要使用 [UIImage imageNamed:nil]; 这种方式,如果使用这种方式加载,只要程序不退出,它便一直会在内存中。
我们可以使用 :
NSString *path = [[NSBundlemainBundle]pathForResource:@'"图片的名字" ofType:@""];
UIImage *image = [UIImageimageWithContentsOfFile:path];
那两者的优缺点就很明显了,[UIImage imageNamed:nil]; 只需加载一次,它便在内存中,所以第二次加载速度很快。而第二种加载方式由于我们将它释放掉了,会再次加载。所以选用那种方式,依你情况而定。
2,上面说的第二种方式,虽然可以释放掉,但我们要告诉人家什么时候释放。也就是说,当前显示页面不是这个view时,我们便将它释放掉:
- (void)viewWillDisappear:(BOOL)animated{
[UIImageView removeFromSuperview];
UiImageView = nil;
}
当然,当我们再次进入这个view时,便要将移除掉的view再次添加进来
- (void)viewDidAppear:(BOOL)animated{
[self addSubView:UIImageView];
}
3,上述两种方式,主要解决内存累加的问题。但如果第一次进入view,图片全部渲染在view上时,内存就崩溃了。那我们只能在图片上做文章了。我们加载的高清大图如果差不多都是3000*2000,也可能比这个还大,就算我们的程序是iPad App,iPad 4 的分辨率才多少,这些图远远大于设备的分辨率,完全是资源浪费,所以我们通常的一个做法,便是将这样的图以小尺寸渲染到view上。
推荐使用:
UIImage+Resize.h, UIImage+Resize.mExtends the UIImage class to support resizing (optionally preserving the original aspect ratio), cropping, and generating thumbnails.
UIImage+RoundedCorner.h, UIImage+RoundedCorner.mExtends the UIImage class tosupport adding
rounded corners to an image.
UIImage+Alpha.h, UIImage+Alpha.mExtends the UIImage class with helper methods for working with alpha layers
(transparencies).
常用方法:
UIImage *image
UIImage *thumbImage = [imagethumbnailImage:140// This should the size of the view in collection view. example: myCell width is 20 and height is 20.
transparentBorder:0
cornerRadius:0
interpolationQuality:kCGInterpolationMedium]; //生成缩略图
// this "resizedimage" image is what you want to pass to setImage
UIImage * resizedImage = [imageresizedImage:imageview.frame.sizeinterpolationQuality: kCGInterpolationLow]; //生成你想要尺寸的图
造成的问题,要注意缩放的比例,不要导致图片变形,由于尺寸缩小,可能会导致图片模糊,注意缩小的尺寸。
综上可见,每种方法有优点,有缺点。主要依据自己的开发情况,折中使用。
相关文章推荐
- OpenLayers 3 之 加载天地图
- Python + OpenCV模糊处理(Bluring)
- ubuntu14.04 install tomcat
- How to format a JavaScript date(jquery ,json ,日期)
- 岁月划过生命线——记我的大学生活(前言)
- matlab画甘特图(用任意多颜色表示任意多工件)
- Linux多线程实践(8) --Posix条件变量解决生产者消费者问题
- drawable中绘制双重的背景
- LeetCode---Power of Two
- 定时任务简单实例
- 优化UITableViewCell高度计算的那些事
- UML——活动图,状态图
- 类型对指针运算的影响
- 巴伦电路基础
- [LeetCode] Delete Node in a Linked List
- 51nod 1270 数组的最大代价 (DP_好题)
- 在按一次退出App
- 平行二叉堆和优先队列
- 如何把一个字符串格式化位4位一个空格
- 使用Ant构建项目