图像裁剪
程序员文章站
2022-03-24 19:59:28
...
因为这里的背景的黑色,所以使用不透明。裁剪图片的代码:
- (UIImage *)clipImage:(UIImage *)image toSize:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
CGSize imgSize = image.size;
CGFloat x = MAX(size.width / imgSize.width, size.height / imgSize.height);
CGSize resultSize = CGSizeMake(x * imgSize.width, x * imgSize.height);
[image drawInRect:CGRectMake(0, 0, resultSize.width, resultSize.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
异步去剪裁图片,在剪裁完成后再更新:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIImage *image = [UIImage imageNamed:model.imageName];
image = [self clipImage:image toSize:self.imageView.frame.size];
dispatch_async(dispatch_get_main_queue(), ^{
model.clipedImage = image;
self.imageView.layer.contents = (__bridge id _Nullable)(model.clipedImage.CGImage);
});
});