在绘制成NSImage时设置颜色
问题描述:
我试图在可可中用日期标记图片文件,并试图在小命令行工具中执行此操作。它工作正常... 但,我似乎无法设置颜色。难道我做错了什么?在绘制成NSImage时设置颜色
#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;
}
答
我相信你需要在drawAtPoint使用NSForegroundColorAttributeName属性来设置文本的颜色:withAttributes:打电话,而不是setStroke/setFill。
文本的颜色是黑色而不是白色?否则,问题可能在其他地方。 – 2009-05-03 19:55:38
全部是黑色的。我也尝试添加[NSBezierPath fillRect:rect];这也是全黑的... – epatel 2009-05-03 20:32:20