5
votes

Imagine I have an UIImage. I need to rotate and then crop it in the global coordinate system (not UIImage coordinate system). So the resulted image will be cropped and rotated.

How can I do this? CGImageCreateWithImageInRect will crop image only in the image-relative coordinates.

enter image description here

PS Answer in comments. You should use CIImage.

2

2 Answers

2
votes

Maybe this can help

//init
CIImage *image = [CIImage imageWithCGImage:CGImageRef];
//rotation
CIImage *rotatedImage = [image imageByApplyingTransform:someTransform];
//crop
CIImage *croppedImage = [rotatedImage imageByCroppingToRect:someRect];
0
votes

For rotation, you need to apply

 CGAffineTransform transform = CGAffineTransformTranslate(self.imageView.transform,deltaX,deltaY);
 transform = CGAffineTransformRotate(transform, recognizer.rotation);
 transform = CGAffineTransformTranslate(transform, -deltaX, -deltaY);
 self.imageView.transform = transform;

For cropping, simply change the frame of your imageView

To convert local coordinates'image, simply use :

// converts a point in local coordinate space to window coordinates. 
[self.imageView convertPoint:localPosition toView:nil];

// use this method to calculate a view's origin in window space like this:
[self.imageView convertPoint:self.imageView.frame.origin toView:nil];

If you plan to integrate an image editor to your app, you may wish to consider using a third party like : https://github.com/heitorfr/ios-image-editor