Rotate and crop UIImage

Imagine I have a UIImage. I need to rotate and then crop it in the global coordinate system (and not in the UIImage coordinate system). Thus, the image will be cropped and .

How can i do this? CGImageCreateWithImageInRect trims the image only in image coordinates.

enter image description here

PS Answer in the comments. You must use CIImage.

+5
source share
2 answers

Maybe this will help

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

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; 

To crop, simply resize your image.

To convert local coordinates just use:

 // converts a point in local coordinate space to window coordinates. [self.imageView convertPoint:localPosition toView:nil]; // use this method to calculate a view origin in window space like this: [self.imageView convertPoint:self.imageView.frame.origin toView:nil]; 

If you plan to integrate the image editor into your application, you can consider using a third party, for example: https://github.com/heitorfr/ios-image-editor

0
source

Source: https://habr.com/ru/post/1202486/


All Articles