简体   繁体   English

UIImageView基于UIView掩码的图像裁剪

[英]UIImageView image crop based on UIView mask

So i have an canvas (UIView) and a UIImageView, the canvas acts as a mask over the imageview 所以我有一个画布(UIView)和一个UIImageView,画布充当了imageview上的一个遮罩

在此输入图像描述

i am using UIGestureRecognizers to zoom and rotate the UIImageView which is under the canvas. 我正在使用UIGestureRecognizers来缩放和旋转画布下的UIImageView。

i want to convert the final image (show in the canvas to a UIImage, one solution is to convert the canvas to an image like below 我想转换最终图像(在画布中显示为UIImage,一种解决方案是将画布转换为如下图像

UIGraphicsBeginImageContext(self.canvas.bounds.size);
[self.canvas.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newCombinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

now this works fine but the problem with this solution is the image is cropped to the dimensions of the canvas so the resolution is very low. 现在这个工作正常,但这个解决方案的问题是图像被裁剪为画布的尺寸,因此分辨率非常低。

another option i explored was to use some custom UIImage categories to rotate and scale. 我探索的另一个选择是使用一些自定义UIImage类别来旋转和缩放。

[[[self.photoImage image] imageRotatedByDegrees:rotaton_angle] 
     imageAtRect:CGRectMake(x,y width,height)]

i need to provide rotation angle (the rotation angle provided by UIGesture Delegate is not in Degrees or Radians, then there is x,y,width,height, i imagine these needs to be calculated based on some scale, (i do get scale value from UIGesture delegate but they do not seem to be correct for this function) 我需要提供旋转角度(UIGesture Delegate提供的旋转角度不是度数或弧度,然后有x,y,宽度,高度,我想这些需要根据某种尺度计算,(我确实得到了比例值)来自UIGesture代表,但他们似乎没有正确的这个功能)

there are a number of solutions here, that guides you to crop and image given a rect. 这里有很多解决方案,可以指导你给出一个矩形的裁剪和图像。 but in my case the rect is not the same scale as the image also there is rotation involved. 但在我的情况下,矩形与图像的尺度不同,也涉及旋转。

any help will be appreciated. 任何帮助将不胜感激。

i have managed to solve this, here is my solution, it most definitely isn't the cleanest, but it works. 我设法解决了这个问题,这是我的解决方案,它绝对不是最干净的,但它确实有效。

i needed to handle 3 things, pan, zoom and rotate. 我需要处理3件事,平移,缩放和旋转。

firstly i used the the UIGestureRecognizer Delegate to get cumulative values incrementing at UIGestureRecognizerStateEnded for all 3. 首先,我使用UIGestureRecognizer委托来获取所有3的UIGestureRecognizerStateEnded累积值。

then for rotation i just used the UIImage Category discussed here 然后为了轮换我刚刚使用了这里讨论的UIImage类别

    self.imagetoEdit = [self.imagetoEdit imageRotatedByRadians:total_rotation];

for zooming (scale) i used GPUImage (i am using this throughout the app) 缩放(缩放)我使用GPUImage(我在整个应用程序中使用此功能)

GPUImageTransformFilter *scaleFilter = [[GPUImageTransformFilter alloc] init];
[scaleFilter setAffineTransform:CGAffineTransformMakeScale(total_scale, total_scale)];
[scaleFilter prepareForImageCapture];
self.imagetoEdit = [scaleFilter imageByFilteringImage:self.imagetoEdit];

for panning, im doing this. 为了平移,我这样做。 (Not the cleanest code :S ) also using the above mention UIImage+Categories. (不是最干净的代码:S)也使用上面提到的UIImage + Categories。

CGFloat x_ = (translation_point.x/canvas.frame.size.width)*self.imagetoEdit.size.width;
CGFloat y_ = (translation_point.y/canvas.frame.size.height)*self.imagetoEdit.size.height;
CGFloat xx = 0;
CGFloat yy = 0;
CGFloat ww = self.imagetoEdit.size.width-x_;
CGFloat hh = self.imagetoEdit.size.height-y_;

if (translation_point.x < 0) {
    xx = x_*-1;
    ww = self.imagetoEdit.size.width + xx;
}

if (translation_point.y < 0) {
    yy = y_*-1;
    hh = self.imagetoEdit.size.height + yy;
}

CGRect cgrect = CGRectMake(xx,yy, ww, hh);
self.imagetoEdit = [self.imagetoEdit imageAtRect:cgrect];

everything seem to work. 一切似乎都有效。

This might be helpful... Resizing a UIImage the right way 这可能会有所帮助...... 以正确的方式调整UIImage的大小

It might need some updating for ARC etc... though I think there are people who have done it and have posted it on Github. 可能需要对ARC等进行一些更新......虽然我认为有人已经完成并将其发布在Github上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM