在开发中会有这样的场景,从网上下载图片,压缩图片到屏幕尺寸,最后显示出来,显示的位置或许是CollectionView上,在整个过程中,还会有滑动,Cell 重用等情况。如果不处理好,会有些许卡顿。
// 线程同步方式去下载图片, 会阻塞线程
NSURL *url = [NSURL URLWithString:@"http://img0.imgtn.bdimg.com/it/u=86408331,3792352527&fm=23&gp=0.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
注意的是,这个方法是阻塞线程的,如果使用主线程,凑巧网络不给力,那么界面会卡顿,通常会用异步下载。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSURL *url = [NSURL URLWithString:@"http://img0.imgtn.bdimg.com/it/u=86408331,3792352527&fm=23&gp=0.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
// 下载好后继续 裁剪图片
。。。
});
图片获取到之后,然后开始裁剪,这里的imageMaxSize 指的是最长边,这个方法的好处在于解码峰值把握得非常好,性能可能不是最优,CGContext 相关的缩图方案可以查查google或者baidu,很完善了。
// 直接压缩,没有峰值 , 需要自己释放 CGImageRef
CGImageRef thumbnailImageWithData(NSData *jpgData, NSInteger imageMaxSize) {
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)jpgData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : @(imageMaxSize)
};
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options);
CFRelease(imageSource);
return thumbnail;
}
我尝试用 3500 x 6225像素的图片压缩成 359x640,效果还是不错的,另一种方案也能压缩,他们2种都是大约0.42s(iPhone7),不过第2种可调参数更多。
- (CGImageRef)resizeImageAtPathFast:(NSString *)imagePath
{
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
CGImageRef imageRef = img.CGImage;
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, 359, 640));
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
CGImageGetBitsPerComponent(imageRef),
0,
CGImageGetColorSpace(imageRef),
CGImageGetBitmapInfo(imageRef));
// Rotate and/or flip the image if required by its orientation
CGContextConcatCTM(bitmap, CGAffineTransformIdentity);
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(bitmap, kCGInterpolationDefault);
CGContextSetShouldAntialias(bitmap, true);
CGContextSetAllowsAntialiasing(bitmap, true);
//CGContextSetFlatness(bitmap, 0);
// Draw into the context; this scales the image
//CGContextDrawTiledImage(bitmap, transposedRect, imageRef);
CGContextDrawImage(bitmap, newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
return newImageRef;
}
kCGInterpolationDefault 这个地方有几个选择,如果选择Low 可以快很多,质量会差很多,清楚得看到锯齿。注意,这个方法其实没有对exif里的方向作处理。
接下来是显示,一般情况下使用了UIImageView这个控件来显示图片,如果说这个图片不需要有事件的响应的话可以直接用 CALayer,
CALayer *x = [[CALayer alloc] init];
x.frame = CGRectMake(x, y, w, h);
/** Layer `contentsGravity' values.
CA_EXTERN NSString * const kCAGravityCenter
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityTop
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityBottom
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityLeft
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityRight
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityTopLeft
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityTopRight
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityBottomLeft
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityBottomRight
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityResize
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityResizeAspect
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAGravityResizeAspectFill
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
**/
// 图片的显示模式,这里使用 等比全部显示完
x.contentsGravity = kCAGravityResizeAspect;
x.masksToBounds = YES;// 裁剪多余
x.contents =(__bridge id _Nullable)(cgImg);//cgImg 最好已经解码好了,速度最快
[self.view.layer addSublayer:x];
完