Question

J'essaie de baliser un fichier image avec une date dans Cocoa et j'essaie de le faire dans un petit outil en ligne de commande. Cela fonctionne très bien ... mais , je n'arrive pas à définir la couleur. Est-ce que je fais quelque chose de mal?

#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;
}
Était-ce utile?

La solution

Je pense que vous devez définir la couleur du texte à l'aide de l'attribut NSForegroundColorAttributeName dans drawAtPoint: withAttributes: call, plutôt que setStroke / setFill.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top