将通过手势 UIImageView 与另一个旋转的 previosly 合并.WYS 不是 WYG [英] Merging a previosly rotated by gesture UIImageView with another one. WYS is not WYG

查看:11
本文介绍了将通过手势 UIImageView 与另一个旋转的 previosly 合并.WYS 不是 WYG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试合并两个 UIImageView 时快疯了.

I'm getting crazy while trying to merge two UIImageView.

情况:

  • 一个背景 UIImageView(userPhotoImageView)
  • 叠加UIImageView (productPhotoImageView)可以拉伸,捏合和旋转

我在 UIImages 上调用我的函数,但我可以从包含它们的 UIImageView 中获取坐标和新的拉伸大小(它们在我的类中合成)

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;

也许最简单的方法是在新的 CGContextRef 中渲染顶部 UIImageView 的图层,如下所示:

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.

第二种方法是尝试将 AffineTransformation 应用于 UIImage 以重现 GestureEffects,然后在上下文中绘制它,如下所示:

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

第二种方法的问题是,自从用户开始交互后,我无法准确获得最终的旋转度数(应在上面的代码中填写的 ANGLE 参数)量,这是因为 RotationGesture 已重置应用后为 0,因此下一个回调是当前旋转的增量.

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.

当然,最简单的方法是第一种,冻结两个 UIImageView,因为它们在那一刻显示,但实际上我仍然没有找到这样做的方法.

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.

推荐答案

太好了,这很有帮助,而且解决方法太多;)

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:

- (UIImage *)mergeImage:(UIImage *)bottomImg withImage:(UIImage *)topImg {

    UIImage * scaledTopImg = [topImg imageByScalingProportionallyToSize:productPhotoView.frame.size];

    UIGraphicsBeginImageContext(scaledTopImg.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, scaledTopImg.size.width * 0.5f, scaledTopImg.size.height  * 0.5f);    
    CGFloat angle = atan2(productPhotoView.transform.b, productPhotoView.transform.a);    
    CGContextRotateCTM(ctx, angle);    
    [scaledTopImg drawInRect:CGRectMake(- scaledTopImg.size.width * 0.5f, -(scaledTopImg.size.height  * 0.5f), scaledTopImg.size.width, scaledTopImg.size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginImageContext(bottomImg.size);
    [bottomImg drawInRect:CGRectMake(0, 0, bottomImg.size.width, bottomImg.size.height)];    
    [newImage drawInRect:CGRectMake(productPhotoView.frame.origin.x, productPhotoView.frame.origin.y, newImage.size.width, newImage.size.height)];
    UIImage *newImage2 = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();    

    return newImage2;
}

因为我得到了背景图片,所以首先创建一个上下文只是为了将转换应用于覆盖视图,而不是保存它并写在底层的顶部.

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.

在旋转覆盖视图之前注意 CTM 平移,否则它将围绕位于 {0,0} 的轴旋转,而我想围绕其中心旋转图像.

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.

问候

这篇关于将通过手势 UIImageView 与另一个旋转的 previosly 合并.WYS 不是 WYG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆