绘制到NSImage时设置颜色

时间:2009-05-03 19:27:16

标签: cocoa png

我正在尝试使用Cocoa中的日期标记图片文件,并尝试在小命令行工具中执行此操作。它工作得很好...... 但是,我似乎无法设置颜色。我做错了吗?

#import <Cocoa/Cocoa.h>

int main (int argc, const char * argv[]) {
    [NSApplication sharedApplication];
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:
                [NSString stringWithFormat:@"%s", "/some/file.png"]];

    if (image) {
        [image lockFocus];
        NSColor *color = [NSColor whiteColor];
        // THESE DOESN'T SEEM TO WORK...
        [color set];
        [color setStroke];
        [color setFill];
        NSString *string = [NSString stringWithFormat:@"%@", [NSDate date]];
        [string drawAtPoint:NSMakePoint(10, 10) withAttributes:nil];
        [image unlockFocus];

        NSBitmapImageRep *bits = [NSBitmapImageRep imageRepWithData:
                                    [image TIFFRepresentation]];

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

        [data writeToFile:@"/some/file.png"
               atomically:NO];
    }
    [pool drain];
    return 0;
}

1 个答案:

答案 0 :(得分:2)

我认为你需要使用drawAtPoint:withAttributes:call中的NSForegroundColorAttributeName属性来设置文本的颜色,而不是setStroke / setFill。