gpt4 book ai didi

ios - 无法理解iOS核心图形坐标系转换,求助

转载 作者:可可西里 更新时间:2023-11-01 03:57:28 27 4
gpt4 key购买 nike

我正在尝试了解 quartz 2d 坐标系,目前我正在阅读 Apple 引用指南和旧书“Quartz 2d graphics for mac os x developer”。

我理解用户空间和设备空间的概念,设备空间可以有不同的默认坐标系,设备空间坐标不能修改,我们通过修改其坐标系来映射用户空间设备空间以达到期望的结果。


  • 问题的第一部分

Quartz 2d graphics for mac os x developer book 说:

When transforming a coordinate system, you must have another coordinate system to compare it to. The transformation provides a relative mapping from one coordinate system to the other. When you draw in the transformed coordinate system, the transformation maps the graphic back to the first coordinate system. The appearance of that graphic in the fixed coordinate system is affected by the transformation.

我没有用粗体表示这一点。

Quartz 2D programming guide说:

Quartz accomplishes device independence with a separate coordinate system—user space—mapping it to the coordinate system of the output device—device space—using the current transformation matrix, or CTM. A matrix is a mathematical construct used to efficiently describe a set of related equations. The current transformation matrix is a particular type of matrix called an affine transform, which maps points from one coordinate space to another by applying translation, rotation, and scaling operations (calculations that move, rotate, and resize a coordinate system).

The current transformation matrix has a secondary purpose: It allows you to transform how objects are drawn. For example, to draw a box rotated by 45 degrees, you rotate the coordinate system of the page (the CTM) before you draw the box. Quartz draws to the output device using the rotated coordinate system.

令人困惑的是“Quartz 使用旋转坐标系绘制到输出设备。”,如果我想绘制一个旋转的对象(图像等)而其他不旋转,那么会发生什么?我们有旋转的整个坐标,每一个东西都会被旋转?

我正在尝试不同的实验,但无法解决这个问题,我创建了一个图像,我在 photoshop 中用两条线复制了左下角的坐标系,然后添加到我的项目中,通过调用 可以看到坐标的行为方式CGContextRotateCTM(myContext, 45); 在 drawrect 方法中,但它没有对我使用界面生成器将图像放置在 uiimage 中的 xib 文件中包含的图像做任何事情。

这段代码来自quartz 2D编程指南

CGContextRef myContext = UIGraphicsGetCurrentContext();
CGRect contextRect = self.bounds;

CGContextTranslateCTM(myContext, 0, contextRect.size.height);
CGContextRotateCTM(myContext, 45); //my modification
CGContextScaleCTM(myContext, 1, -1);

float w, h;
w = contextRect.size.width;
h = contextRect.size.height;

CGAffineTransform myTextTransform;
CGContextSelectFont (myContext, "Helvetica-Bold", h/10, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (myContext, 10);
CGContextSetTextDrawingMode (myContext, kCGTextFillStroke);

CGContextSetRGBFillColor (myContext, 0, 1, 0, .5);
CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1);
myTextTransform = CGAffineTransformMakeRotation(0);
CGContextSetTextMatrix (myContext, myTextTransform);
CGContextShowTextAtPoint (myContext, 0, 50, "Quartz 2D", 9);

但是这段代码修改的是屏幕上绘制的文本而不是我添加的图像?


  • 问题的第二部分

这也是来自 Quartz 2D 编程指南:

... apply a transform that translates the origin to the upper-left corner of the PDF context and scales the y-coordinate by -1.

Using a scaling transform to negate the y-coordinate alters some conventions in Quartz drawing. For example, if you call CGContextDrawImage to draw an image into the context, the image is modified by the transform when it is drawn into the destination. ...

我已经在做这个 -1 的事情,但对图像没有影响,这个 -1 的事情我仍然不清楚。

我已经多次阅读这份文件,尝试在谷歌上搜索,但没有有用的教程,也没有最新的书籍,只有 2004、2005、2006 年写的书籍。任何人都可以提供帮助或可以向我推荐有用的资源来学习这很深入。

苦苦等待回复,真的需要帮助。谢谢。

最佳答案

关于ios - 无法理解iOS核心图形坐标系转换,求助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9408498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com