gpt4 book ai didi

ios - 基于 UIView mask 的 UIImageView 图像裁剪

转载 作者:技术小花猫 更新时间:2023-10-29 10:52:58 26 4
gpt4 key购买 nike

所以我有一个 Canvas (UIView) 和一个 UIImageView, Canvas 充当 imageview 的 mask

enter image description here

我正在使用 UIGestureRecognizers 缩放和旋转 Canvas 下的 UIImageView。

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

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

现在这个工作正常,但这个解决方案的问题是图像被裁剪到 Canvas 的尺寸,所以分辨率非常低。

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

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

我需要提供旋转角度(UIGesture Delegate提供的旋转角度不是度数或弧度,然后是x,y,width,height,我想这些需要根据一些比例来计算,(我做从 UIGesture 委托(delegate)获取比例值,但它们似乎不适合此功能)

这里有许多解决方案,可指导您根据给定的矩形裁剪和图像。但在我的例子中,矩形与图像的比例不同,也涉及旋转。

任何帮助将不胜感激。

最佳答案

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

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

首先,我使用 UIGestureRecognizer Delegate 获取所有 3 个在 UIGestureRecognizerStateEnded 递增的累积值。

然后为了轮换,我只使用了讨论的 UIImage 类别 here

    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];

似乎一切正常。

关于ios - 基于 UIView mask 的 UIImageView 图像裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15518296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com