IOS 多个UIImageView 加载高清大图时内存管理
来源:互联网 发布:红梅白雪知 伴奏 编辑:程序博客网 时间:2024/04/30 08:21
当我们在某一个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];
}
推荐使用:
- UIImage+Resize.h, UIImage+Resize.m
- Extends the UIImage class to support resizing (optionally preserving the original aspect ratio), cropping, and generating thumbnails.
- UIImage+RoundedCorner.h, UIImage+RoundedCorner.m
- Extends the UIImage class to support adding rounded corners to an image.
- UIImage+Alpha.h, UIImage+Alpha.m
- Extends 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]; //生成你想要尺寸的图
造成的问题,要注意缩放的比例,不要导致图片变形,由于尺寸缩小,可能会导致图片模糊,注意缩小的尺寸。
综上可见,每种方法有优点,有缺点。主要依据自己的开发情况,折中使用。
- IOS 多个UIImageView 加载高清大图时内存管理
- IOS 多个UIImageView 加载高清大图时内存管理
- iOS 多个UIImageView 加载高清大图时的内存管理
- IOS 多个UIImageView 加载高清大图时内存管理
- IOS多个UIImageView加载高清大图时内存管理
- IOS 多个UIImageView 加载高清大图时内存管理
- iOS开发经验之多个UIImageView 加载高清大图时内存管理
- UIImageView 加载高清大图时内存管理
- 多个ImageView 加载高清大图的内存管理
- AsyncLoadLocalImage ios 加载本地高清大图
- Android 加载高清大图
- 使用SDWebimage加载高清大图遇到内存警告的问题
- iOS 加载大量高清图片, 内存警告导致crash
- 通过webView全屏加载高清大图片,解决不能全屏高清加载图片问题
- 一招解决Android 加载高清大图
- Android 高清加载长图或大图方案
- Android中加载高清大图及图片压缩方式
- RunLoop UITableViewCell加载高清大图的速度优化
- GtkWidget中下拉框右侧有多于的白颜色部分的经验总结
- DFS--障碍在指定时间会消失
- git log 参数大补
- IE10弹窗showModalDialog关闭之后提示SCRIPT5011:不能执行已释放的Script代码
- 《编码:隐匿在计算机软硬件背后的语言》读书笔记 06
- IOS 多个UIImageView 加载高清大图时内存管理
- JSTL国际化;SQL;XML;函数标签库
- 使用JS/Jquery获得父窗口的几个方法(笔记)
- jsp 中文参数乱码问题
- 第16周项目1-数组的排序
- ios创建开关Button
- 谈谈项目测试管理
- Android Studio关于SVN的相关配置简介
- cenots下网卡配置