【问题标题】:Flipped NSString drawing in CGContext在 CGContext 中翻转 NSString 绘图
【发布时间】:2012-07-01 17:56:47
【问题描述】:

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

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

我已经画好了线:

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

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

PS:对不起我的英语;)

【问题讨论】:

    标签: iphone


    【解决方案1】:

    正如here 所述,与普通 Quartz 绘图空间相比,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);
    

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

    【讨论】:

      【解决方案2】:

      绘制前最好保存文本的矩阵,否则会影响CoreGraphic绘制的其他文本:

      CGContextSaveGState(ctx);
      CGAffineTransform save = CGContextGetTextMatrix(ctx);
      CGContextTranslateCTM(ctx, 0.0f, self.bounds.size.height);
      CGContextScaleCTM(ctx, 1.0f, -1.0f);
      [str drawAtPoint:point withFont:font];
      CGContextSetTextMatrix(ctx, save);
      CGContextRestoreGState(ctx);
      

      【讨论】:

        猜你喜欢
        • 2021-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多