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

前端 未结 5 1500
灰色年华
灰色年华 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条回答
  •  情深已故
    2020-12-08 07:40

    You can do that by apply affinetransform on the point you want to convert in UIKit related coordinates. Following is example.

    // Create a affine transform object
    CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
    // First translate your image View according to transform
    transform = CGAffineTransformTranslate(transform, 0, - imageView.bounds.size.height);
    // Then whenever you want any point according to UIKit related coordinates apply this transformation on the point or rect.
    // To get tranformed point
    CGPoint newPointForUIKit = CGPointApplyAffineTransform(oldPointInCGKit, transform);
    // To get transformed rect
    CGRect newRectForUIKit = CGRectApplyAffineTransform(oldPointInCGKit, transform);
    

提交回复
热议问题