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

标签 ios graphics cgaffinetransform

我想将 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();

有什么想法可以帮助我吗?非常感谢!

最佳答案

将这两个图像作为 subview 添加到 UIView。对图像执行您需要的操作,然后像这样拍摄 UIVIew 的快照:

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

关于ios - 如何将 2 个 UIImageView 中的 2 个图像合并为 1 个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14039632/

相关文章:

ios - 在 Xcode 4.3 和 iOS 5.1 开发环境上测试 iOS 4.3

python - 用 mayavi 或 vtk 绘制凹形物体

graphics - 我想要没有堆栈且没有递归的Flood Fill

java - FingerPaint 删除显示黑线

ios - 具有负 M_PI 的 CGAffineTransformMakeRotation

iOS状态栏轻触返回App

ios - 设置两个变量后如何调用方法

ios - Swift 2 - iOS - 分派(dispatch)回原始线程

ios - 如何在先前转换的 UIView 上正确地动画转换

ios - 如何从 CGAffineTransformMake 创建 CGAffineTransform?