Rotate and Crop UIImage

这一生的挚爱 提交于 2019-12-07 06:32:07

问题


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.

PS Answer in comments. You should use CIImage.


回答1:


Maybe this can help

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



回答2:


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



来源:https://stackoverflow.com/questions/25822730/rotate-and-crop-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!