在CGContext中翻转NSString绘图

Chi*_*odo 3 iphone

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

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

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

    colorSpace = CGColorSpaceCreateDeviceRGB();
data = malloc(height * width * 4);
context = CGBitmapContextCreate(data, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
Run Code Online (Sandbox Code Playgroud)

我画了字符串:

    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();
Run Code Online (Sandbox Code Playgroud)

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

PS:对不起我的英文;)

Bra*_*son 7

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

解决此问题的一种方法是保存图形状态,对图像应用反转变换,绘制文本以及恢复旧图形状态.这将类似于以下内容:

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);
Run Code Online (Sandbox Code Playgroud)

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