在CGContext中翻转NSString绘图 [英] Flipped NSString drawing in CGContext

查看:102
本文介绍了在CGContext中翻转NSString绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在此纹理中绘制一个字符串:

I try to draw a string in this texture:

http://picasaweb.google.it/lh/photo/LkYWBv_S_9v2d6BAfbrhag?feat=directlink

但是绿色数字似乎垂直翻转。
我用这种方式创建了我的上下文:

but the green numbers seem vertical flipped. I've created my context in this way:

    colorSpace = CGColorSpaceCreateDeviceRGB();
data = malloc(height * width * 4);
context = CGBitmapContextCreate(data, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

我画了字符串:

    UIGraphicsPushContext(context);

for(int i=0;i<packs.size();++i)
{
    CGPoint points[4] = 
    {
        gltextures[i].texCoords[0] * size.width,        //0
        gltextures[i].texCoords[1] * size.height,       //1

        gltextures[i].texCoords[2] * size.width,        //2
        gltextures[i].texCoords[3] * size.height,       //3

        gltextures[i].texCoords[4] * size.width,        //4
        gltextures[i].texCoords[5] * size.height,       //5

        gltextures[i].texCoords[6] * size.width,        //6
        gltextures[i].texCoords[7] * size.height        //7

    };

    CGRect debugRect = CGRectMake
    (
     gltextures[i].texCoords[0] * size.width, 
     gltextures[i].texCoords[1] * size.height, 
     gltextures[i].width, 
     gltextures[i].height
     );
    CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextAddLines(context, points, 4);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathStroke);  

    NSString* s = [NSString stringWithFormat:@"%d",gltextures[i].texID];
    UIFont* font = [UIFont fontWithName:@"Arial" size:12];
    CGContextSetRGBFillColor(context, 0, 1, 0, 1);
    [s drawAtPoint:points[0] withFont:font];
}
UIGraphicsPopContext();

转换矩阵似乎是Identity矩阵...没有CGAffineTransform应用于此上下文。
如果字符串被翻转,也许,我的所有图像都被翻转了!
有什么建议吗?

The transformation matrix seems the Identity matrix... no CGAffineTransform is applied to THIS context. If the string is flipped, maybe, all my images are flipped! Any suggestion?

PS:对不起我的英文;)

PS: sorry for my english ;)

推荐答案

此处所述,与普通的Quartz绘图空间相比,UIView或其图层中的上下文的坐标系被反转。但是,当使用Quartz绘制图像时,情况就不再如此。 NSString上的UIStringDrawing Cocoa Touch类别为您提供了-drawAtPoint:withFont:方法假定了一个反转的布局,这就是您在图像中看到翻转文本的原因。

As describe here, the coordinate system for a context within a UIView or its layer is inverted when compared to the normal Quartz drawing space. However, when using Quartz to draw to an image, this is no longer the case. The UIStringDrawing Cocoa Touch category on NSString that gives you the -drawAtPoint:withFont: method assumes an inverted layout, and that's why you're seeing the flipped text in your image.

解决此问题的一种方法是保存图形状态,对图像应用反转变换,绘制文本以及恢复旧的图形状态。这看起来如下所示:

One way to work around this would be to save the graphics state, apply an invert transform to the image, draw the text, and restore the old graphics state. This would look something like the following:

CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0f, self.bounds.size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);

[text drawAtPoint:CGPointMake(0.0f, 0.0f) withFont:font];

CGContextRestoreGState(context);

这是一个相当粗略的例子,因为我认为它只是在底部绘制文本图像,但您应该能够调整文本的平移或坐标以将其放置在需要的位置。

This is a fairly crude example, in that I believe it just draws the text at the bottom of the image, but you should be able to adjust the translation or the coordinate of the text to place it where it's needed.

这篇关于在CGContext中翻转NSString绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆