简体   繁体   English

iOS倒置坐标混乱

[英]iOS inverted coordinates chaos

So I am using CGBitmapContext to adapt a software blitter to iOS. 所以我使用CGBitmapContext来适应iOS的软件阻塞。 I write my data to the bitmap, then I use CoreGraphics functions to write text over the CGBitmapContext. 我将数据写入位图,然后使用CoreGraphics函数在CGBitmapContext上写入文本。 The software blitter uses Upper Left Hand Origin, while the CoreGraphics functions use Lower Left Hand Origin. 软件阻击器使用左上手原点,而CoreGraphics功能使用左下手原点。 So when I draw an image using the blitter at (0, 0), it appears in the upper left hand corner. 因此,当我使用(0,0)的阻击器绘制图像时,它出现在左上角。 When I draw text using CG at (0, 0) it appears in the lower left hand corner and it is NOT INVERTED. 当我在(0,0)处使用CG绘制文本时,它出现在左下角并且它没有被反转。 Yes I have read all the other questions about inverting coordinates and I am doing that to the the resulting CG image to display it in the view properly. 是的我已经阅读了有关反转坐标的所有其他问题,我正在对生成的CG图像执行此操作以在视图中正确显示它。 Perhaps a code example would help... 也许代码示例会有所帮助......

    // This image is at the top left corner using the custom blitter.
screen->write(image, ark::Point(0, 0));
// This text is at the bottom left corner RIGHT SIDE UP
// cg context is a CGBitmapContext
CGContextShowTextAtPoint(
    cgcontext, 
    0, 0, 
    str.c_str(), 
    str.length());

// Send final image to video output.
CGImageRef screenImage = CGBitmapContextCreateImage(cgcontext);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, 480);
// Here I flip the whole image, so if I flip the text above, then
// it ends up upside down.
CGContextScaleCTM(context, 1, -1); 
CGContextDrawImage(context, 
    CGRectMake(0, 0, 320, 480), screenImage);
CGContextRestoreGState(context);

CGImageRelease(screenImage);

How can I mix coordinate systems? 如何混合坐标系? How can I draw my text right side up with ULC origin? 如何用ULC原点正确地绘制我的文本?

Flipping the co-ordinate system means you will draw upside down. 翻转坐标系统意味着您将颠倒。 You can't do that transformation without that result. 没有这个结果,你不能做那个转变。 (The reason to do it is when you're going to hand your result off to something that will flip it back, so that upside down will be right-side-up again.) (这样做的原因是当你将你的结果转移到可以将其翻转的东西上时,所以颠倒将会再次正面向上。)

Everything always draws with positive y going up and negative y going down. 一切在积极的y上升,负的y下降。 The text rises toward positive y, and descends below the baseline toward negative y. 文本向正y上升,并从基线下降到负y。 Consecutive lines' baselines have lower and lower y positions. 连续线的基线具有较低和较低的y位置。 The same is true of images and (non-textual) paths—everything draws this way. 图像和(非文本)路径也是如此 - 一切都是这样的。

What you do by transforming the co-ordinate system is not change how things draw; 你通过改变坐标系来做什么并不会改变事物的绘制方式; they always draw with positive y going up. 他们总是以积极的态度上升。 To transform the co-ordinate system is to redefine up and down . 改变协调系统是重新定义上下 You make positive y into negative y, and vice versa. 你将正y变为负y,反之亦然。 You also change where 0, 0 is. 你也改变0,0的位置。

So what you need to do is not to flip the co-ordinate system, but to (only) translate it. 所以你需要做的不是翻转坐标系,而是(仅)翻译它。 You need to translate up by the entire height of the context, minus the height of a line of text. 您需要按上下文的整个高度进行平移,减去一行文本的高度。 Then, with 0,0 defined as that position, you can show your text at 0,0 and it will draw from that position, and the right way up (and down). 然后,将0,0定义为该位置,您可以在0,0处显示文本,它将从该位置绘制,并向右(和向下)向右绘制。

Or, don't translate, and just show the text from that point. 或者,不要翻译,只显示该点的文字。 Either way will work. 无论哪种方式都可行。

I'm pretty sure you don't need and won't want any transformation in the destination context. 我很确定你不需要也不希望在目标上下文中进行任何转换。 The translation (if you translate) should happen in the bitmap context. 翻译(如果翻译)应该在位图上下文中进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM