繁体   English   中英

UIImageView基于UIView掩码的图像裁剪

[英]UIImageView image crop based on UIView mask

所以我有一个画布(UIView)和一个UIImageView,画布充当了imageview上的一个遮罩

在此输入图像描述

我正在使用UIGestureRecognizers来缩放和旋转画布下的UIImageView。

我想转换最终图像(在画布中显示为UIImage,一种解决方案是将画布转换为如下图像

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

现在这个工作正常,但这个解决方案的问题是图像被裁剪为画布的尺寸,因此分辨率非常低。

我探索的另一个选择是使用一些自定义UIImage类别来旋转和缩放。

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

我需要提供旋转角度(UIGesture Delegate提供的旋转角度不是度数或弧度,然后有x,y,宽度,高度,我想这些需要根据某种尺度计算,(我确实得到了比例值)来自UIGesture代表,但他们似乎没有正确的这个功能)

这里有很多解决方案,可以指导你给出一个矩形的裁剪和图像。 但在我的情况下,矩形与图像的尺度不同,也涉及旋转。

任何帮助将不胜感激。

我设法解决了这个问题,这是我的解决方案,它绝对不是最干净的,但它确实有效。

我需要处理3件事,平移,缩放和旋转。

首先,我使用UIGestureRecognizer委托来获取所有3的UIGestureRecognizerStateEnded累积值。

然后为了轮换我刚刚使用了这里讨论的UIImage类别

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

缩放(缩放)我使用GPUImage(我在整个应用程序中使用此功能)

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

为了平移,我这样做。 (不是最干净的代码: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];

一切似乎都有效。

这可能会有所帮助...... 以正确的方式调整UIImage的大小

可能需要对ARC等进行一些更新......虽然我认为有人已经完成并将其发布在Github上。

暂无
暂无

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

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