一个图片解码成未压缩的位图的时候,占用的内存和图片的文件大小没有关系,和图片的尺寸大小有关系,尺寸越大,所需要的像素点越多,所以超大图片,我们需要进行优化处理
这里直接上降采样代码
给UIImage 添加一个分类,在分类中添加一个方法 ,该size就是
UIImageView的大小
- (UIImage *)resizeCG:(CGSize)size
{
CGFloat maxPixelSize = MAX(size.width, size.height);
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)UIImageJPEGRepresentation(self, 0.9), nil);
NSDictionary *options = @{(__bridge id)kCGImageSourceCreateThumbnailFromImageAlways:(__bridge id)kCFBooleanTrue,
(__bridge id)kCGImageSourceThumbnailMaxPixelSize:[NSNumber numberWithFloat:maxPixelSize]
};
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)options);
UIImage *newImage = [UIImage imageWithCGImage:imageRef scale:2 orientation:self.imageOrientation];
CGImageRelease(imageRef);
CFRelease(sourceRef);
return newImage;
}