您的位置:首页 > 产品设计 > UI/UE

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]; //生成你想要尺寸的图

造成的问题,要注意缩放的比例,不要导致图片变形,因为尺寸缩小,可能会导致图片模糊,注意缩小的尺寸。

综上可见。每种方法有长处,有缺点。主要根据自己的开发情况,用妥协。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: