Impostazione del colore quando si disegna in un NSImage
Domanda
Sto cercando di taggare un file di immagine con una data in Cocoa e sto provando a farlo in un piccolo strumento da riga di comando. Funziona bene ... ma , non riesco a impostare il colore. Sto facendo qualcosa di sbagliato?
#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;
}
Soluzione
Credo che tu debba impostare il colore del testo usando l'attributo NSForegroundColorAttributeName in drawAtPoint: withAttributes: call, piuttosto che setStroke / setFill.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow