如何补偿核心图形的翻转坐标系以便于绘图? [英] How to compensate the flipped coordinate system of core graphics for easy drawing?

查看:93
本文介绍了如何补偿核心图形的翻转坐标系以便于绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的很痛苦,但总是当我在-drawRect中绘制UIImage时,它是颠倒的。

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

当我翻转坐标时,图像正确绘制,但是所有其他CG功能的成本都是错误(翻转)。

When I flip the coordinates, the image draws correctly, but at the cost of all other CG functions drawing "wrong" (flipped).

当你必须绘制图像和其他东西时,你的策略是什么?有没有经验法则如何不要一次又一次地陷入这个问题呢?

What's your strategy when you have to draw images and other things? Is there any rule of thumb how to not get stuck in this problem over and over again?

另外,当我翻转y轴时,一件令人讨厌的事情是,我的来自UIImageView框架的CGRect是错误的。而不是原点出现在预期的10,10左上方,它出现在底部。

Also, one nasty thing when I flip the y-axis is, that my CGRect from the UIImageView frame is wrong. Instead of the origin appearing at 10,10 upper left as expected, it appears at the bottom.

但与此同时,CGContext的所有正常线条绘制功能都采取了正确的坐标。在-drawRect中绘制一条线,原点10,10左上方,将从左上角开始。但同时这很奇怪,因为核心图形实际上有一个翻转坐标系统,底部是y 0。

But at the same time, all those normal line drawing functions of CGContext take correct coordinates. drawing a line in -drawRect with origin 10,10 upper left, will really start at upper left. But at the same time that's strange, because core graphics actually has a flipped coordinate system with y 0 at the bottom.

所以看起来某些东西真的不一致。使用CGContext函数绘制时,坐标为预期(cmon,没有人认为坐标从左下角开始,这是愚蠢的),而绘制任何类型的图像仍然是错误的方式。

So it seems like something is really inconsistent there. Drawing with CGContext functions takes coordinates as "expected" (cmon, nobody thinks in coordinates starting from bottom left, that's silly), while drawing any kind of image still works the "wrong" way.

您是否使用辅助方法绘制图像?或者有什么有用的东西可以使图像绘制不是屁股上的痛苦吗?

Do you use helper methods to draw images? Or is there anything useful that makes image drawing not a pain in the butt?

推荐答案

问题:原点位于左下角;正y向上(负y向下)。
目标:起源于左上角;正y向下(负y向上)。

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).

解决方案:


  1. 将原点向上移动视图的高度。

  2. 否定(乘以-1)y轴。

在代码中执行此操作的方法是翻译由视图边界的高度和按比例缩放(1,-1)。

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

Quartz 2D Programming有几部分内容与本主题相关的指南,包括绘制到图形上下文在iPhone OS和整个变换章节。当然,你真的应该阅读整篇文章。

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.

这篇关于如何补偿核心图形的翻转坐标系以便于绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆