How to get the correctly region to crop an UIImage?

169 views Asked by At

I have a UIImageView in my objective c application. On this UIImageView I load a picture then take this from camera. I apply a region to crop the UIImage. This region is a rectangle by CAShapeLayer. But my problem is that when I get the frame contained in rectangle, this isn't of the same position where is my rectangle... I'm getting a wrong region then crop.

I use this code:

 CAShapeLayer *_outline = [CAShapeLayer new];
 UIImageView *vistaImagen;
 ...
UIGraphicsBeginImageContextWithOptions(_vistaImagen.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddPath(context, _boundingBox.accessibilityPath.CGPath);

CGContextClip(context);
[_vistaImagen.layer renderInContext:context];
UIImage *iii = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

What could be the problem, if I have the frame of my layer to crop added: _boundingBox.accessibilityPath.CGPath ??

Thanks!!

1

There are 1 answers

0
user3745888 On

Well, After spending a few hours, I have found the problem. I was creating the UIGraphicsBeginImageContextWithOptions with a bad scale... I need use scale to 1 because I'm getting the image in UIImage in real scale, but I had the scale initialized to 0.

UIImageView *vistaImagen;
UIGraphicsBeginImageContextWithOptions(_vistaImagen.frame.size, NO, 1.0);