iOS倒坐标混乱

发布于 2024-10-18 06:00:03 字数 1142 浏览 4 评论 0原文

所以我使用 CGBitmapContext 来使软件 blitter 适应 iOS。我将数据写入位图,然后使用 CoreGraphics 函数在 CGBitmapContext 上写入文本。软件 blitter 使用左上原点,而 CoreGraphics 功能使用左下手原点。因此,当我使用 (0, 0) 处的位块传输器绘制图像时,它会出现在左上角。当我使用 CG 在 (0, 0) 处绘制文本时,它出现在左下角并且不是反转的。是的,我已经阅读了有关反转坐标的所有其他问题,并且我正在对生成的 CG 图像执行此操作,以便在视图中正确显示它。也许代码示例会有所帮助...

    // 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);

如何混合坐标系?如何以 ULC 原点将文本正面朝上绘制?

So I am using CGBitmapContext to adapt a software blitter to iOS. I write my data to the bitmap, then I use CoreGraphics functions to write text over the CGBitmapContext. The software blitter uses Upper Left Hand Origin, while the CoreGraphics functions use Lower Left Hand Origin. So when I draw an image using the blitter at (0, 0), it appears in the upper left hand corner. When I draw text using CG at (0, 0) it appears in the lower left hand corner and it is NOT INVERTED. 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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖阳 2024-10-25 06:00:03

翻转坐标系意味着您将上下颠倒地绘制。如果没有这个结果,你就无法进行这种转换。 (这样做的原因是当你要把你的结果交给一个能把它翻转回来的东西时,这样颠倒的东西就会再次变成正立的。)

一切都总是绘制正 y 上升,负 y 下降。文本向正 y 方向上升,向负 y 方向下降到基线以下。连续线的基线具有越来越低的 y 位置。图像和(非文本)路径也是如此——一切都是这样绘制的。

通过变换坐标系所做的并不是改变事物的绘制方式;而是改变事物的绘制方式。他们总是以正 y 向上的方式绘制。 变换坐标系就是重新定义上下。将正 y 变为负 y,反之亦然。您还可以更改 0, 0 的位置。

所以你需要做的不是翻转坐标系,而是(仅)平移它。您需要向上平移上下文的整个高度,减去一行文本的高度。然后,将 0,0 定义为该位置,您可以在 0,0 处显示文本,它将从该位置开始绘制,并以正确的方式向上(和向下)。

或者,不翻译,只显示该点的文本。无论哪种方式都会起作用。

我很确定您不需要也不希望在目标上下文中进行任何转换。翻译(如果您进行翻译)应该发生在位图上下文中。

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. The text rises toward positive y, and descends below the baseline toward negative y. Consecutive lines' baselines have lower and lower y positions. 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. You also change where 0, 0 is.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文