多个ImageView 加载高清大图的内存管理
来源:互联网 发布:mac能下bilibili吗 编辑:程序博客网 时间:2024/04/30 08:45
当我们在某一个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]; //生成你想要尺寸的图
造成的问题,要注意缩放的比例,不要导致图片变形,由于尺寸缩小,可能会导致图片模糊,注意缩小的尺寸。
综上可见,每种方法有优点,有缺点。主要依据自己的开发情况,折中使用。
- 多个ImageView 加载高清大图的内存管理
- iOS 多个UIImageView 加载高清大图时的内存管理
- IOS 多个UIImageView 加载高清大图时内存管理
- IOS 多个UIImageView 加载高清大图时内存管理
- IOS 多个UIImageView 加载高清大图时内存管理
- IOS多个UIImageView加载高清大图时内存管理
- IOS 多个UIImageView 加载高清大图时内存管理
- iOS开发经验之多个UIImageView 加载高清大图时内存管理
- UIImageView 加载高清大图时内存管理
- Android 加载高清大图
- 使用SDWebimage加载高清大图遇到内存警告的问题
- RunLoop UITableViewCell加载高清大图的速度优化
- AsyncLoadLocalImage ios 加载本地高清大图
- Android ImageView加载摄像头拍摄的大图
- 关于Andorid加载高清大图(仿sina weibo 里面的长图效果)
- 通过webView全屏加载高清大图片,解决不能全屏高清加载图片问题
- 一招解决Android 加载高清大图
- Android 高清加载长图或大图方案
- 基于opencv的相机之camera切换/闪光灯/边缘模式/分辨率(二)
- 定时任务简单实例
- Linux多线程实践(8) --Posix条件变量解决生产者消费者问题
- drawable中绘制双重的背景
- Java路径问题最终解决方案—可定位所有资源的相对路径寻址
- 多个ImageView 加载高清大图的内存管理
- Python + OpenCV模糊处理(Bluring)
- OpenLayers 3 之 加载天地图
- 教你快速在c#中调用C++代码(函数)
- tcpdump指令抓取组播数据
- fragment嵌套的问题
- 二叉树先序遍历动态演示——python版
- NSUserDefaults简介及使用
- java编码