将之前通过手势旋转的 UIImageView 与另一个合并。 WYS 不是 WYG
我在尝试合并两个 UIImageView 时变得疯狂。
情况:
- 一个背景UIImageView (userPhotoImageView)
- 一个覆盖的 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,因为它们在那一刻显示,但实际上我仍然没有找到这样做。
I'm getting crazy while trying to merge two UIImageView.
The situation:
- a background UIImageView
(userPhotoImageView) - an overlayed
UIImageView (productPhotoImageView)
that can be streched, pinched and
rotated
I call my function on the UIImages but I can take coords and new stretched size from the UIImageView containing them (they are synthesized in my class)
- (UIImage *)mergeImage:(UIImage *)bottomImg withImage:(UIImage *)topImg;
Maybe simplest way would be rendering the layer of the top UIImageView in a the new CGContextRef like this:
[bottomImg drawInRect:CGRectMake(0, 0, bottomImg.size.width, bottomImg.size.height)];
[productPhotoImageView.layer renderInContext:ctx];
But in this way I loose the rotation effect previosly applied by gestures.
A second way would be trying to apply AffineTransformation to UIImage to reproduce GestureEffects and then draw it in the context like this:
UIImage * scaledTopImg = [topImg imageByScalingProportionallyToSize:productPhotoView.frame.size];
UIImage * rotatedScaledTopImg = [scaledTopImg imageRotatedByDegrees:ANGLE];
[rotatedScaledTopImg drawAtPoint:CGPointMake(productPhotoView.frame.origin.x, productPhotoView.frame.origin.y)];
The problem of this second approach is that I'm not able to exactly get the final rotation degrees (the ANGLE parameter that should be filled in the code above) amount since the user started to interact, this because the RotationGesture is reset to 0 after applying so the next callback is a delta from the current rotation.
For sure the most easy way would be the first one, freezing the two UIImageViews as they are displayed in that very moment but actually I still didn't find anyway to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,基本上还有另一种解决方法可以解决所有这些疯狂的合并问题,但这绝对不是一个优雅的解决方案。为了避免处理任何类型的 AffineTransformation,只需捕获 ImageScreen 然后裁剪它。
告诉过你这并不优雅,但对于某人来说可能很有用。
Ok basically there is another workaround for all this crazy merging stuff, but it's definitively not an elegant solution. For avoid handling any kind of AffineTransformation just capture the ImageScreen and then crop it.
Told you it wasnt elegant, but for someone it could be useful.
太棒了,这很有帮助,而且解决方法太多了;)
所以,因为我发现在我的操作之后很难找到一些代码示例来合并图片,希望它能有所帮助:
因为我得到了背景图像,所以首先更容易创建一个上下文只是为了将转换应用于叠加视图,而不是保存它并写入底层。
在旋转叠加视图之前请注意 CTM 平移,否则它将围绕位于 {0,0} 的轴旋转,而我想围绕其中心旋转图像。
问候
Great that was helpful and so too much workaroundy ;)
so since i see it's pretty hard to find around some code examples to merge pics after a manipulation here goes mine, hope it can be helpful:
Since I got a background image it was easier first create a context just to apply tranformation to the overlayview, than save it and write on top of the bottom layer.
Take care of the CTM translation before rotating the overlayview or it will rotate around an axis placed at {0,0} while I want to rotate my image around its center.
Regards
我有同样的问题。
我只有旋转方面的问题。
我使用手势识别器进行移动、缩放和旋转。
开始时,当我保存图像时,比例是正确的,位置是正确的,但从未对保存的图像应用旋转。
现在,
如果您想知道旋转角度,我可以使用:
和旋转:
如果您有不同的方法,请告诉我。
I have the same problem.
I only have problem with rotation.
I use gesture recognizers for move, scale and rotation.
At the beginning when I save the image tha scale was right, the position was right but never apply rotation to the saved image.
Now it works
if you want to know the rotation angle I get it with:
And rotation with:
If you have a different approach please let me know.