gpt4 book ai didi

cocoa - 绘制到 NSImage 时设置颜色

转载 作者:行者123 更新时间:2023-12-03 17:45:50 25 4
gpt4 key购买 nike

我正在尝试在 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;
}

最佳答案

我相信您需要在drawAtPoint:withAttributes:调用中使用NSForegroundColorAttributeName属性来设置文本的颜色,而不是setStroke/setFill。

关于cocoa - 绘制到 NSImage 时设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/817792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com