Extract a part of UIImageView(提取部分 UIImageView)
问题描述
我想知道是否可以提取" UIImageView
的一部分.
I was wondering if it's possible to "extract" a part of UIImageView
.
例如,我使用 Warp Affine 选择 UIImageView
的一部分,并且我知道所选部分 frame
.
For example, I select using Warp Affine a part of the UIImageView
and I know the selected part frame
.
喜欢这张图片:
是否可以从原始UIImageView
中只获取选定的部分而不损失质量?
Is it possible to get from the original UIImageView
only the selected part without losing quality?
推荐答案
通过category方法获取视图的快照:
Get the snapshot of the view via category method:
@implementation UIView(Snapshot)
-(UIImage*)makeSnapshot
{
CGRect wholeRect = self.bounds;
UIGraphicsBeginImageContextWithOptions(wholeRect.size, YES, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, wholeRect);
[self.layer renderInContext:ctx];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
然后通过另一种类别方法将其裁剪到您的矩形:
then crop it to your rect via another category method:
@implementation UIImage(Crop)
-(UIImage*)cropFromRect:(CGRect)fromRect
{
fromRect = CGRectMake(fromRect.origin.x * self.scale,
fromRect.origin.y * self.scale,
fromRect.size.width * self.scale,
fromRect.size.height * self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, fromRect);
UIImage* crop = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
CGImageRelease(imageRef);
return crop;
}
@end
在你的 VC 中:
UIImage* snapshot = [self.imageView makeSnapshot];
UIImage* imageYouNeed = [snapshot cropFromRect:selectedRect];
selectedRect
应该在你的 self.imageView
坐标系中,如果没有那么使用selectedRect = [self.imageView convertRect:selectedRect fromView:...]
selectedRect
should be in you self.imageView
coordinate system, if no so then use
selectedRect = [self.imageView convertRect:selectedRect fromView:...]
这篇关于提取部分 UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!