iOS中的內(nèi)存管理十分重要,有時候頁面的卡頓、App的崩潰都與內(nèi)存有關(guān)。
在平時的開發(fā)過程中,都會使用到大量的圖片。在處理高精度高分辨率的圖片時,因為圖片質(zhì)量過大,有時候需要進(jìn)行壓縮處理。
生成縮略圖的五種方式:
- UIKit
- Core Graphics
- Core Image
- ImageIO
- vImage
UIKit
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[UIImage drawInRect:]在繪制時,先解碼圖片,再生成原始分辨率大小的bitmap,這是很耗內(nèi)存的。應(yīng)該避免中間bitmap產(chǎn)生
Core Graphics
YYImage和SDWebImage都是使用這種方法。解壓縮的原理就是CGBitmapContextCreate方法重新生產(chǎn)一張位圖然后把圖片繪制當(dāng)這個位圖上,最后拿到的圖片就是解壓縮之后的圖片。
下面是一部分代碼
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); CGContextRef context = CGBitmapContextCreate(nil,
size.width,
size.height,
bytePerComponent,
bytePerRow,
colorSpace,
bitmapInfo);
//設(shè)置插值質(zhì)量
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
//繪圖
CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), imageRef);
//生成imageRef
CGImageRef bitmapImageRef = CGBitmapContextCreateImage(context);
UIImage *image = [UIImage imageWithCGImage:bitmapImageRef scale:self.scale orientation:self.imageOrientation];
Core Image
CIImage *ciImageInput = [CIImage imageWithCGImage:imageRef];
CIFilter *filter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
[filter setValue:ciImageInput forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithDouble:scale] forKey:kCIInputScaleKey];
[filter setValue:@(1.0) forKey:kCIInputAspectRatioKey];
CIImage *ciImageOutput = [filter valueForKey:kCIOutputImageKey];
if (!ciImageOutput) {
return nil;
}
CIContext *ciContext = [[CIContext alloc] initWithOptions:@{kCIContextUseSoftwareRenderer : @(NO)}];
CGImageRef ciImageRef = [ciContext createCGImage:ciImageOutput fromRect:CGRectMake(0, 0, size.width, size.height)];
CoreImage是五種方式里面,性能最差的,一般不用。
ImageIO
ImageIO是一個獨立的圖像處理框架,使用時需要先#import<ImageIO/ImageIO.h>。
ImageIO的功能非常強(qiáng)大,但是也更加底層,代碼一般使用CF類來編寫,可以單獨去學(xué)習(xí)這一框架,這里只用來處理縮略圖
//獲取原圖片屬性
CFDictionaryRef property = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil);
NSDictionary *propertys = CFBridgingRelease(property);
CGFloat height = [propertys[@"PixelHeight"] integerValue]; //圖像k寬高,12000
CGFloat width = [propertys[@"PixelWidth"] integerValue];
//以較大的邊為基準(zhǔn)
int imageSize = (int)MAX(size.width, size.height);
CFStringRef keys[5];
CFTypeRef values[5];
//創(chuàng)建縮略圖等比縮放大小,會根據(jù)長寬值比較大的作為imageSize進(jìn)行縮放
//kCGImageSourceThumbnailMaxPixelSize為生成縮略圖的大小。當(dāng)設(shè)置為800,如果圖片本身大于800*600,則生成后圖片大小為800*600,如果源圖片為700*500,則生成圖片為800*500
keys[0] = kCGImageSourceThumbnailMaxPixelSize;
CFNumberRef thumbnailSize = CFNumberCreate(NULL, kCFNumberIntType, &imageSize);
values[0] = (CFTypeRef)thumbnailSize;
keys[1] = kCGImageSourceCreateThumbnailFromImageAlways;
values[1] = (CFTypeRef)kCFBooleanTrue;
keys[2] = kCGImageSourceCreateThumbnailWithTransform;
values[2] = (CFTypeRef)kCFBooleanTrue;
keys[3] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
values[3] = (CFTypeRef)kCFBooleanTrue;
keys[4] = kCGImageSourceShouldCacheImmediately;
values[4] = (CFTypeRef)kCFBooleanTrue;
CFDictionaryRef options = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CGImageRef thumbnailImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options);
UIImage *resultImg = [UIImage imageWithCGImage:thumbnailImage];
vImage
vImage也是Accelerate庫的一部分,使用時需要先#import <Accelerate/Accelerate.h>,側(cè)重于高性能的圖像Bitmap級別的處理。庫本身全部是C的接口,而且不同于Core系列的(Core Graphics/Core Foundation)C接口,是比較貼近傳統(tǒng)C語言的接口,不會有Ref這種貼心的定義,而且很多接口需要自己手動分配內(nèi)存。
//定義ARGB8888的結(jié)構(gòu)體格式
vImage_CGImageFormat format;
format.bitsPerComponent = 8;
format.bitsPerPixel = 32; //ARGB四通道 4*8
format.colorSpace = nil; //默認(rèn)sRGB
format.bitmapInfo = kCGImageAlphaFirst | kCGBitmapByteOrderDefault; // 表示ARGB
format.version = 0;
format.decode = nil; //默認(rèn)色彩映射范圍【0, 1.0】
format.renderingIntent = kCGRenderingIntentDefault;//超出【0,1】范圍后怎么處理
//源圖片buffer,輸出圖片buffer
vImage_Buffer sourceBuffer, outputBuffer;
vImage_Error error = vImageBuffer_InitWithCGImage(&sourceBuffer, &format, nil, imageRef, kvImageNoFlags);
if (error != kvImageNoError) {
return nil;
}
float scale = self.scale;
int width = (int)size.width;
int height = (int)size.height;
int bytesPerPixel = (int)CGImageGetBitsPerPixel(imageRef)/8;
//設(shè)置輸出格式
outputBuffer.width = width;
outputBuffer.height = height;
outputBuffer.rowBytes = bytesPerPixel * width;
outputBuffer.data = malloc(outputBuffer.rowBytes * outputBuffer.height);
//縮放到當(dāng)前尺寸上
error = vImageScale_ARGB8888(&sourceBuffer, &outputBuffer, nil, kvImageHighQualityResampling);
if (error != kvImageNoError) {
return nil;
}
CGImageRef outputImageRef = vImageCreateCGImageFromBuffer(&outputBuffer, &format, nil, nil, kvImageNoFlags, &error);
vImage給我的感覺與VideoToolBox的解碼方式比較類似,不過這里使用起來太過麻煩了。
以上就是iOS中對圖片進(jìn)行縮略的五種解碼方式。
內(nèi)容主要是在學(xué)習(xí)了http://www.itdecent.cn/p/de7b6aede888以后整理的,文中有關(guān)于5種方式的性能分析。
在寫完本章的demo以后,代碼中主要對Core Graphics和ImageIO的方式進(jìn)行了加載時間和內(nèi)存的測試。
這里是demo