我在尝试合并两个 UIImageView 时变得疯狂。
情况:
- 背景 UIImageView
(用户照片图像视图)
- 一个重叠的
UIImageView(产品照片图像视图)
可以拉伸、挤压和
旋转的
我在 UIImages 上调用我的函数,但我可以从包含它们的 UIImageView 中获取坐标和新的拉伸尺寸(它们在我的类中合成)
- (UIImage *)mergeImage:(UIImage *)bottomImg withImage:(UIImage *)topImg;
也许最简单的方法是在新的 CGContextRef 中渲染顶部 UIImageView 的层,如下所示:
[bottomImg drawInRect:CGRectMake(0, 0, bottomImg.size.width, bottomImg.size.height)];
[productPhotoImageView.layer renderInContext:ctx];
但这样我就失去了之前通过手势应用的旋转效果。
第二种方法是尝试将 AffineTransformation 应用于 UIImage 来重现 GestureEffects,然后在上下文中绘制它,如下所示:
UIImage * scaledTopImg = [topImg imageByScalingProportionallyToSize:productPhotoView.frame.size];
UIImage * rotatedScaledTopImg = [scaledTopImg imageRotatedByDegrees:ANGLE];
[rotatedScaledTopImg drawAtPoint:CGPointMake(productPhotoView.frame.origin.x, productPhotoView.frame.origin.y)];
第二种方法的问题是,自用户开始交互以来,我无法准确获得最终的旋转度数(应在上面的代码中填写的 ANGLE 参数)量,这是因为 RotationGesture 在之后重置为 0应用,以便下一个回调是当前轮换的增量。
当然,最简单的方法是第一个,冻结两个 UIImageViews,因为它们在那一刻显示,但实际上我仍然没有找到这样做。