如何将2个UIImageView中的2个图像合并为1个图像

o0o*_*o0o 2 graphics cgaffinetransform ios

我想将2个UIImageView中的2个图像合并为1个图像,其中"正确"的位置,角度,比例比例如我在屏幕上看到的那样.

在我的项目中,有2个UIImageView - imageView是主要的UIImageView - topImageView是可以拖动,旋转,缩放的叠加UIImageView

我可以保存图像合并绘制2图像但错误的位置,角度,比例尺度.

这是我的代码:

UIImage *img = topImageView.image;
UIImage *bottomImg = self.imageView.image;
CGSize bounds = self.imageView.image.size;

UIGraphicsBeginImageContext(bounds);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, topImageView.image.size.width * 0.5, topImageView.image.size.height * 0.5);
CGFloat angle = atan2(topImageView.transform.b, topImageView.transform.a);
CGContextRotateCTM(ctx, angle);

[topImageView.image drawInRect:CGRectMake(-topImageView.image.size.width * 0.5,
                                          -topImageView.image.size.height * 0.5,
                                          topImageView.image.size.width,
                                          topImageView.image.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIGraphicsBeginImageContext(bounds);
[bottomImg drawInRect:CGRectMake(0, 0, bounds.width, bounds.height)];
[newImage drawInRect:CGRectMake(topImageView.frame.origin.x,
                                topImageView.frame.origin.y,
                                newImage.size.width,
                                newImage.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

有什么好主意帮我吗?非常感谢!

Kri*_*lci 6

将这两个图像作为子视图添加到UIView.做你需要的图像,然后像这样拍摄UIVIew的快照:

UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *MergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)