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

前端 未结 5 1492
灰色年华
灰色年华 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:57

    The better answer to this problem is to use the UIImage method drawInRect: to draw your image. I'm assuming you want the image to span the entire bounds of your view. This is what you'd type in your drawRect: method.

    Instead of:

    CGContextRef ctx = UIGraphicsGetCurrentContext();  
    UIImage *myImage = [UIImage imageNamed:@"theImage.png"];  
    CGImageRef img = [myImage CGImage];
    CGRect bounds = [self bounds];
    CGContextDrawImage(ctx, bounds, img);
    

    Write this:

    UIImage *myImage = [UIImage imageNamed:@"theImage.png"];
    CGRect bounds = [self bounds];
    [myImage drawInRect:bounds];
    

提交回复
热议问题