Giu*_*nco 1 iphone resize objective-c uiimageview warp
我想知道是否有可能"提取"一部分UIImageView
.
例如,我选择使用Warp Affine作为其中一部分,UIImageView
我知道所选部分frame
.
像在这张图片中:
是否有可能从原来UIImageView
只有选定的部分获得而不会失去质量?
通过类别方法获取视图的快照:
@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
Run Code Online (Sandbox Code Playgroud)
然后通过另一种类别方法将其裁剪到您的rect:
@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
Run Code Online (Sandbox Code Playgroud)
在你的VC中:
UIImage* snapshot = [self.imageView makeSnapshot];
UIImage* imageYouNeed = [snapshot cropFromRect:selectedRect];
Run Code Online (Sandbox Code Playgroud)
selectedRect
应该在你的self.imageView
坐标系中,如果不是那么就用
selectedRect = [self.imageView convertRect:selectedRect fromView:...]
归档时间: |
|
查看次数: |
3336 次 |
最近记录: |