2009-05-03 104 views
0

我試圖在可可中用日期標記圖片文件,並試圖在小命令行工具中執行此操作。它工作正常... ,我似乎無法設置顏色。難道我做錯了什麼?在繪製成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; 
} 
+0

文本的顏色是黑色而不是白色?否則,問題可能在其他地方。 – 2009-05-03 19:55:38

+0

全部是黑色的。我也嘗試添加[NSBezierPath fillRect:rect];這也是全黑的... – epatel 2009-05-03 20:32:20

回答

2

我相信你需要在drawAtPoint使用NSForegroundColorAttributeName屬性來設置文本的顏色:withAttributes:打電話,而不是setStroke/setFill。