IPhone: How to use CGContextConcatCTM to properly save the converted image?

I am making an iPhone application that downloads an image from a camera, and then the user can select the second image from the library, move / scale / rotate this second image, and then save the result. I use two UIImageViews in IB as placeholders, and then apply touch / pinch transforms.

The problem arises when I need to save both images together. I use the size rectangle of the first image and pass it to UIGraphicsBeginImageContext. Then I tried to use CGContextConcatCTM, but I can’t figure out how this 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 stored in the img2.transform matrix? The documentation for CGContextConcatCTMnot really helps me. Now I'm trying to solve this by calculating the points and angle using trigonometry (using this answer ), but since there is a transformation there, to be a simpler and more elgetic way to do this, right?

+3
source share
1 answer

Take a look at this great answer, you need to create a raster / context image, draw it in and get the resulting image. Then you can save it. iOS UIImagePickerController - result of image orientation after loading

0

Source: https://habr.com/ru/post/1738941/


All Articles