tags:

views:

316

answers:

1

I'm trying to tag a picture file with a date in Cocoa and are trying to do this in a small command line tool. It works fine...but, I can't seem to be able to set the color. Am I doing something wrong?

#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;
}
+2  A: 

I believe you need to set the color of text using the NSForegroundColorAttributeName attribute in the drawAtPoint:withAttributes: call, rather than setStroke/setFill.

smorgan
Will try. But the set/setFill didn't change the color for a [NSBezierPath fillRect:rect]; everything was black...
epatel
Worked great! Thanks! :)
epatel