1

我有一个 void 绘制到通常在自定义视图的 drawRect 例程中提供的给定 CGContextRef 但是我还需要生成它可以绘制到的 CGContextRef 将允许我将生成的图像保存为 PNG 文件.

4

1 回答 1

4

事实证明这很简单:

NSImage *toSave = [[NSImage alloc] initWithSize:CGSizeMake(600, 900)];

[toSave lockFocus];

CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];

//drawing code

[toSave unlockFocus];

NSBitmapImageRep *imgRep = [[toSave representations] objectAtIndex: 0];

NSData *data = [imgRep representationUsingType: NSPNGFileType properties: nil];

[data writeToFile: @"/path/to/file.png" atomically: NO];
于 2012-05-30T18:20:27.663 回答