代码之家  ›  专栏  ›  技术社区  ›  epatel

在绘制nsImage时设置颜色

  •  0
  • epatel  · 技术社区  · 16 年前

    我试图用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;
    }
    
    1 回复  |  直到 16 年前
        1
  •  2
  •   smorgan    16 年前

    我认为您需要使用drawAtpoint:WithAttributes:Call中的nsforgerRoundColorAttributeName属性设置文本的颜色,而不是使用setStroke/setFill。

    推荐文章