Multiple Clipped Rects to Create Collage in Core Graphics

后端 未结 2 808
天命终不由人
天命终不由人 2021-02-02 03:46

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,         


        
相关标签:
2条回答
  • 2021-02-02 04:33

    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));
    
    0 讨论(0)
  • 2021-02-02 04:46

    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.

    0 讨论(0)
提交回复
热议问题