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

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

    Problem: Origin is at lower-left corner; positive y goes upward (negative y goes downward).
    Goal: Origin at upper-left corner; positive y going downward (negative y going upward).

    Solution:

    1. Move origin up by the view's height.
    2. Negate (multiply by -1) the y axis.

    The way to do this in code is to translate up by the view bounds' height and scale by (1, -1), in that order.

    There are a couple of portions of the Quartz 2D Programming Guide that are relevant to this topic, including “Drawing to a Graphics Context on iPhone OS” and the whole chapter on Transforms. Of course, you really should read the whole thing.

提交回复
热议问题