IOS 多于UIImageView 当加载较大的高清闪存管理
2015-08-14 20:59
435 查看
当我们是一家人View 多于UIImageView,和UIImageView表明一个更大的HD,可能存在的存储器的警告的问题。假设第一次走进这个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 to support 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 to support 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]; //生成你想要尺寸的图
造成的问题,要注意缩放的比例,不要导致图片变形,因为尺寸缩小,可能会导致图片模糊,注意缩小的尺寸。
综上可见。每种方法有长处,有缺点。主要根据自己的开发情况,用妥协。
相关文章推荐
- Codeforces Round #316 (Div. 2) D. Tree Requests
- POJ 2524 Ubiquitous Religions
- Repeated DNA Sequences
- hdu 1242 Rescue (BFS+优先队列)
- 【转载】UIScrollView
- HDU 2454 Degree Sequence of Graph G(Havel定理 推断一个简单图的存在)
- J - Guilty Prince
- Codeforces 570D TREE REQUESTS dfs序+树状数组
- 7 Types of Regression Techniques you should know!
- Hadoop之SequenceFile
- hdu - 2667 Proving Equivalences(强连通)
- ListView中requestLayout执行流程解析
- OC - NSValueAndNSNumber
- UIView如何管理它的子视图
- IOS UIGraphicsBeginImageContext 使用简介
- iOS arc与非arc混编 以及设置UINavigationBar的高度
- UI-父视图和子视图之间的关系
- ios UISlider总结
- Django1.8:403错误:CSRF verification failed. Request aborted.
- 在AWS上为1.25亿用户实现移动应用个性化