I am creating a collage combined with elements from other images. Here is some ASCII art to explain what I am doing:
Given images A, B and C,
AAA, BBB, CCC
AAA,
The upside down image can be corrected by calling the below method :
CGImageRef flip (CGImageRef im) {
CGSize sz = CGSizeMake(CGImageGetWidth(im), CGImageGetHeight(im));
UIGraphicsBeginImageContextWithOptions(sz, NO, 0);
CGContextDrawImage(UIGraphicsGetCurrentContext(),
CGRectMake(0, 0, sz.width, sz.height), im);
CGImageRef result = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
UIGraphicsEndImageContext();
return result;
}
Take help from below code as to where u will put this code:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(leftRect.size.width, leftRect.size.height), NO, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, leftRect,flip(leftReference));
CGContextClipToRect
intersects the current clipping rectangle with the argument provided. So the second time you call it, you are effectively turning your clipping region to nothing.
There is no way to restore the clipping region without restoring the graphics state. So, make a call to CGContextSaveGState
at the top of your loop and a call to CGContextRestoreGState
at the bottom.
The upside-down part can be fixed by adjusting the current transformation matrix: call CGContextTranslateCTM
to move the origin and then CGContextScaleCTM
to flip the y-axis.