Extract part of UIImageView

I was wondering if it is possible to “extract” part of the UIImageView .

For example, I select using the Warp Affine part of the UIImageView , and I know the selected part of the frame .

as in this image:

enter image description here

Is it possible to get only the selected part from the original UIImageView without losing quality?

+1
objective-c iphone resize uiimageview warp
source share
2 answers

Get a snapshot of a view using the 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 rectangle using a different 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 

in your VC:

 UIImage* snapshot = [self.imageView makeSnapshot]; UIImage* imageYouNeed = [snapshot cropFromRect:selectedRect]; 

selectedRect should be in your coordinate system self.imageView , if not then use selectedRect = [self.imageView convertRect:selectedRect fromView:...]

+7
source share

Yes it is possible. First you should get the UIImageView image using this property:

 @property(nonatomic, retain) UIImage *image; 

And NSImage:

 @property(nonatomic, readonly) CGImageRef CGImage; 

Then you will get a sectional image:

 CGImageRef cutImage = CGImageCreateWithImageInRect(yourCGImageRef, CGRectMake(x, y, w, h)); 

If you want UIImage again, you should use this UIImage method:

 + (UIImage *)imageWithCGImage:(CGImageRef)cgImage; 

PS: I don’t know how to do this directly, without converting it to CGImageRef, maybe there is a way.

0
source share

All Articles