iPhone:如何使用CGContextConcatCTM正确保存转换后的图像?

发布于 2024-08-26 18:34:33 字数 976 浏览 12 评论 0原文

我正在制作一个 iPhone 应用程序,它从相机加载图像,然后用户可以从库中选择第二张图像,移动/缩放/旋转该第二张图像,然后保存结果。我在 IB 中使用两个 UIImageView 作为占位符,然后在触摸/捏合时应用转换。

当我必须将两个图像保存在一起时,问题就出现了。我使用第一个图像大小的矩形并将其传递给 UIGraphicsBeginImageContext。然后我尝试使用 CGContextConcatCTM ,但我无法理解它是如何工作的:

CGRect rect = CGRectMake(0, 0, img1.size.width, img1.size.height); // img1 from camera
UIGraphicsBeginImageContext(rect.size); // Start drawing
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect); // Clear whole thing
[img1 drawAtPoint:CGPointZero]; // Draw background image at 0,0
CGContextConcatCTM(ctx, img2.transform); // Apply the transformations of the 2nd image

但是接下来我需要做什么? img2.transform 矩阵中保存了哪些信息?不幸的是,CGContextConcatCTM 的文档对我没有多大帮助。 现在我正在尝试通过使用三角学计算点和角度来解决它(在 这个答案),但既然有了转换,就必须有一种更简单、更优雅的方法来做到这一点,对吧?

I am making an iPhone application that loads an image from the camera, and then the user can select a second image from the library, move/scale/rotate that second image, and then save the result. I use two UIImageViews in IB as placeholders, and then apply transformations while touching/pinching.

The problem comes when I have to save both images together. I use a rect of the size of the first image and pass it to UIGraphicsBeginImageContext. Then I tried to use CGContextConcatCTM but I can't understand how it works:

CGRect rect = CGRectMake(0, 0, img1.size.width, img1.size.height); // img1 from camera
UIGraphicsBeginImageContext(rect.size); // Start drawing
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect); // Clear whole thing
[img1 drawAtPoint:CGPointZero]; // Draw background image at 0,0
CGContextConcatCTM(ctx, img2.transform); // Apply the transformations of the 2nd image

But what do I need to do next? What information is being held in the img2.transform matrix? The documentation for CGContextConcatCTM doesn't help me that much unfortunately..
Right now I'm trying to solve it by calculating the points and the angle using trigonometry (with the help of this answer), but since the transformation is there, there has to be an easier and more elgant way to do this, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ繥欢鉨o。 2024-09-02 18:34:33

看看这个优秀的答案,您需要创建一个位图/图像上下文,绘制它,然后得到结果图像。然后您可以保存它。 上传后 iOS UIImagePickerController 结果图像方向

Take a look at this excellent answer, you need to create a bitmapped/image context, draw to it, and get the resultant image out. You can then save that. iOS UIImagePickerController result image orientation after upload

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文