我正在制作一个从相机加载图像的 iPhone 应用程序,然后用户可以从库中选择第二张图像,移动/缩放/旋转第二张图像,然后保存结果。我在 IB 中使用两个 UIImageViews 作为占位符,然后在触摸/捏合时应用转换。
当我必须将两个图像保存在一起时,问题就来了。我使用第一张图像大小的矩形并将其传递给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
我没有多大帮助..现在我正在尝试通过使用三角函数计算点和角度来解决它(在这个答案的帮助下),但是由于转换在那里,所以必须是一种更简单、更优雅的方式来做到这一点,对吧?