How to compensate the flipped coordinate system of core graphics for easy drawing?

前端 未结 5 1506
灰色年华
灰色年华 2020-12-08 07:18

It\'s really a pain, but always when I draw an UIImage in -drawRect:, it\'s upside-down.

When I flip the coordinates, the image draws correctly, but at the cost of a

5条回答
  •  猫巷女王i
    2020-12-08 07:55

    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;
    }
    

    Call the above method using the code below: This code deals with getting the left half of an image from an existing UIImageview and setting the thus generated image to a new imageview - imgViewleft

    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextDrawImage(con,  
                       CGRectMake(0,0,sz.width/2.0,sz.height),
                       flip(leftReference));
    imgViewLeft = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
    

提交回复
热议问题