【问题标题】:Text won't appear in external bitmap/jpeg file. Drawatpoint not working in current context?文本不会出现在外部位图/jpeg 文件中。 Drawatpoint 在当前上下文中不起作用?
【发布时间】:2026-01-20 05:25:01
【问题描述】:

CoreGraphics 相当新,但掌握了基础知识。尝试写入外部文件(不是手机屏幕)。所有网格都会出现,但不会使用 drawAtPoint 呈现文本。无论我尝试什么都做不到!

这是我在 viewDidLoad 时触发的代码:

- (void)viewDidLoad {

    CGFloat width = 312;
    CGFloat height = 482;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    size_t bitsPerComponent = 8;
    size_t bytesPerPixel    = 4;
    size_t bytesPerRow      = (width * bitsPerComponent * bytesPerPixel + 7) / 8;
    size_t dataSize         = bytesPerRow * height;

    unsigned char *data = malloc(dataSize);
    memset(data, 0, dataSize);

    CGContextRef context = CGBitmapContextCreate(data, width, height, 
                                                 bitsPerComponent, 
                                                 bytesPerRow, colorSpace, 
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    // Flip coordinate system
    CGRect bounds = CGContextGetClipBoundingBox(context);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -bounds.size.height);

    //  And now the magic happens...

    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(context, CGRectMake(0,0,312,482));

    int tableTop=54;
    int cellsPerColumn=26;
    int cellHeight=15;

    [@"Title" drawAtPoint:CGPointMake(8, 8) withFont:[UIFont boldSystemFontOfSize:9.0f]];   //  Doesn't print text on  screen?!

    for(int a=1;a<cellsPerColumn+1;a++)
    {
        CGContextStrokeRect(context, CGRectMake(8,(tableTop+cellHeight*(a+1)),60,cellHeight));
        CGContextStrokeRect(context, CGRectMake(66,(tableTop+cellHeight*(a+1)),85,cellHeight));
        CGContextStrokeRect(context, CGRectMake(159,(tableTop+cellHeight*(a+1)),60,cellHeight));
        CGContextStrokeRect(context, CGRectMake(217,(tableTop+cellHeight*(a+1)),85,cellHeight));
    }



    //  Wrap up context and produce the image...



    CGColorSpaceRelease(colorSpace);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *result = [[UIImage imageWithCGImage:imageRef] retain];
    CGImageRelease(imageRef);
    CGContextRelease(context);

    free(data);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"user_images/myImage.jpg"];

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(result)];
    [imageData writeToFile:filePath atomically:YES];
    [super viewDidLoad];
}

我在这个问题上大发雷霆,它需要设置用于渲染文本的上下文或类似的简单内容。非常感谢任何帮助!

谢谢, 丰富

【问题讨论】:

    标签: ios text core-graphics


    【解决方案1】:

    实用方法,例如 drawAtPoint: 仅适用于在当前图形上下文中绘制,通常是在子类 NSView 的 drawRect 回调期间提供的上下文。或者您可以尝试使用您创建的位图上下文的 setCurrentContext: 方法。

    【讨论】:

    • 好的,感谢您的回答,但让我想到也许我正在以错误的方式处理这一切。我正在尝试使用 NS 实用程序命令(例如 drawAtPoint)将图像生成为手机上的文件,以包含绘图对象、图像和文本。该文件的尺寸超出了屏幕的范围,并且该文件需要以编程方式在幕后生成,因此用户不需要看到它。我还想控制生成图像的分辨率(首选 300dpi)。必须有一个简单的方法来做到这一点?非常感谢任何帮助。问候,丰富