如何在 iPhone 上旋转 Quartz 绘制的文本
我想用 Quartz 绘制一些文本。现在应用程序绘制 ,但我希望它显示为“0123456789”。有什么办法可以显示其正常方向吗?
这是我的代码:
//set image view
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = self.view.frame;
[self.view addSubview:drawImage];
UIGraphicsBeginImageContext(self.view.frame.size); // begin
// current context
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSelectFont(context, "Courier", 40, kCGEncodingMacRoman);
CGFloat white[] = {1.0, 1.0, 1.0, 1.0};
CGContextSetFillColor(context, white);
CGContextShowTextAtPoint(context, 0, 30, "0123456789", strlen("0123456789"));
// get image
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); // end
I want to draw some texts using Quartz. Now the app draws
, but I want it to show as "0123456789". Is there any way to show its normal orientation?
Here is my code:
//set image view
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = self.view.frame;
[self.view addSubview:drawImage];
UIGraphicsBeginImageContext(self.view.frame.size); // begin
// current context
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSelectFont(context, "Courier", 40, kCGEncodingMacRoman);
CGFloat white[] = {1.0, 1.0, 1.0, 1.0};
CGContextSetFillColor(context, white);
CGContextShowTextAtPoint(context, 0, 30, "0123456789", strlen("0123456789"));
// get image
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); // end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Quartz 中,图像会上下颠倒,因为它的 Y 轴是倒置的。
iPhone 的 Y 轴从上到下为正,即原点位于左上角,而在 Quartz 和 OpenGL 中,坐标与良好的旧几何体相同,即原点位于左下角。
这里是一些解决 Quartz 反转问题的代码:
这是一篇关于它的有趣文章,作者说新加坡的一个团队由于无法解决这个反转问题而放弃了 iPhone 项目:http://trandangkhoa.blogspot.com/2009/07/iphone-os-drawing -image-and-stupid.html
In Quartz, the image will be upside down because its Y-Axis is inverted.
iPhone's Y axis goes positive from top to bottom i.e. origin is at top left while in Quartz and OpenGL, the co-ordinates are the same way as good old geometry i.e. origin at bottom left.
Here is some code for solving Quartz inversion problem:
Here is an interesting post about it where the author says a team in Singapore gave up an iPhone project due to this inversion problem which they could not figure out: http://trandangkhoa.blogspot.com/2009/07/iphone-os-drawing-image-and-stupid.html
处理这个问题的另一种方法是用类似
文本是 NSString 的代码替换 Quartz 文本绘制代码(从 CGSelectFont() 到 CGContextShowTextAtPoint ())。在 iPhone 上,-drawAtPoint:withFont: 自动翻转文本以考虑 UIViews 的反向 Y 轴。
与纯 Quartz 文本绘图中的 MacRoman 编码相比,这种方法具有处理更广泛字符集的优势。不过,速度稍微慢一些。
Another way to handle this would be to replace the Quartz text drawing code (from CGSelectFont() to CGContextShowTextAtPoint ()) with something like
where text is an NSString. On the iPhone, -drawAtPoint:withFont: automatically flips the text to account for the inverted Y axis of the UIViews.
This approach gives you the advantage of handling a wider character set than the MacRoman encoding in the pure Quartz text drawing can. However, it is slightly slower.
“将它们旋转到正常模式”到底是什么意思?
假设您只是指二维空间内的旋转,通常会使用变换矩阵。看看
CGAffineTransformMakeRotation
。您可以将其应用到视图本身(通过设置
transform
属性),或者应用到您拥有的 CG 上下文。What do you mean, exactly, by "rotate them to the normal mode"?
Assuming you just mean rotation within a 2D space, you would typically use a transform matrix. Have a look at
CGAffineTransformMakeRotation
.You could apply it to the view itself (by setting the
transform
property), or to the CG context you have.好吧,这解决了我的问题
Well this solved my problem