这确实很痛苦,但是当我在 -drawRect: 中绘制 UIImage 时,它总是颠倒的。
当我翻转坐标时,图像绘制正确,但代价是所有其他 CG 函数绘制“错误”(翻转)。
当你必须绘制图像和其他东西时,你的策略是什么?有没有什么经验法则可以避免一次又一次陷入这个问题?
另外,当我翻转 y 轴时,一件令人讨厌的事情是,UIImageView 框架中的 CGRect 是错误的。原点没有像预期那样出现在左上角 10,10 处,而是出现在底部。
但与此同时,CGContext 的所有正常线条绘制函数都采用正确的坐标。在 -drawRect 中以左上角为原点 10,10 绘制一条线,实际上会从左上角开始。但同时这也很奇怪,因为核心显卡实际上有一个翻转的坐标系,y 0 位于底部。
所以看起来确实有些东西不一致。使用 CGContext 函数绘图将坐标视为“预期”(来吧,没有人认为坐标是从左下角开始的,这很愚蠢),而绘制任何类型的图像仍然以“错误”的方式工作。
你使用辅助方法来绘制图像吗?或者有什么有用的东西可以让图像绘制不再是一件痛苦的事?
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 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?
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.
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.
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?
发布评论
评论(5)
问题:原点在左下角;正 y 向上(负 y 向下)。
目标:原点在左上角;正 y 向下(负 y 向上)。
解决方案:
在代码中执行此操作的方法是翻译按视图边界'高度和按 (1, -1) 的顺序缩放。
与本主题相关的 Quartz 2D 编程指南,包括 “在 iPhone OS 上绘制到图形上下文” 以及整个 关于变换的章节。当然,您确实应该阅读整篇文章。
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:
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.
您可以通过在要转换为 UIKit 相关坐标的点上应用 affinetransform 来实现这一点。以下是示例。
You can do that by apply affinetransform on the point you want to convert in UIKit related coordinates. Following is example.
解决此问题的更好方法是使用
UIImage
方法drawInRect:
来绘制图像。我假设您希望图像跨越您的视图的整个边界。这就是您在drawRect:
方法中输入的内容。而不是:
写下:
The better answer to this problem is to use the
UIImage
methoddrawInRect:
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 yourdrawRect:
method.Instead of:
Write this:
您是告诉 UIImage 进行绘制,还是获取其 CGImage 并进行绘制?
如“在 iPhone OS 上绘制到图形上下文”,UIImages 能够意识到坐标空间的差异,并且应该正确地绘制自己,而无需您自己翻转坐标空间。
Are you telling the UIImage to draw, or getting its CGImage and drawing that?
As noted in “Drawing to a Graphics Context on iPhone OS”, UIImages are aware of the difference in co-ordinate spaces and should draw themselves correctly without you having to flip your co-ordinate space yourself.
使用以下代码调用上述方法:
此代码处理从现有 UIImageview 获取图像的左半部分并将由此生成的图像设置为新的 imageview - imgViewleft
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