我正在将一些代码从 Linux 转换到 Mac。
如何以编程方式用文本覆盖图像,类似于 ImageMagick 转换命令?由于各种原因,我不能依赖安装 ImageMagick。
convert -draw 'text 50,800 "hello world"' f1.png f2.png
您可以使用NSImage
and NSString
绘图 API 创建编辑后的图像,然后使用NSBitmapImageRep
获取图像的 PNG 数据。例子:
NSString *text = @"hello world";
NSImage *f1 = [[NSImage alloc] initWithContentsOfFile:@"/path/to/f1.png"];
NSImage *f2 = [NSImage imageWithSize:f1.size flipped:YES drawingHandler:^BOOL(NSRect dstRect) {
[f1 drawInRect:dstRect];
// Attributes can be customized to change font, text color, etc.
NSDictionary *attributes = @{NSFontAttributeName: [NSFont systemFontOfSize:17]};
[text drawAtPoint:NSMakePoint(50, 800) withAttributes:attributes];
return YES;
}];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:[f2 CGImageForProposedRect:NULL context:nil hints:nil]];
NSData *data = [rep representationUsingType:NSPNGFileType properties:nil];
[data writeToFile:@"/path/to/f2.png" atomically:YES];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)