質問

Cocoaで画像ファイルに日付をタグ付けしようとしていますが、これを小さなコマンドラインツールで実行しようとしています。うまくいきます... 、色を設定することができないようです。私は何か間違っていますか?

#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;
}
役に立ちましたか?

解決

setStroke / setFillではなく、drawAtPoint:withAttributes:呼び出しでNSForegroundColorAttributeName属性を使用してテキストの色を設定する必要があると思います。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top