Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image MacOS:以编程方式向图像中添加一些文本?_Image_Macos - Fatal编程技术网

Image MacOS:以编程方式向图像中添加一些文本?

Image MacOS:以编程方式向图像中添加一些文本?,image,macos,Image,Macos,我正在将一些代码从linux转换到mac 如何以编程方式用文本覆盖图像,类似于ImageMagick convert命令?出于各种原因,我不能指望安装ImageMagick convert -draw 'text 50,800 "hello world"' f1.png f2.png 您可以使用NSImage和NSString绘图API创建编辑的图像,然后使用NSBitmapImageRep获取图像的PNG数据。例如: NSString *text = @"hello world"; NSIm

我正在将一些代码从linux转换到mac

如何以编程方式用文本覆盖图像,类似于ImageMagick convert命令?出于各种原因,我不能指望安装ImageMagick

convert -draw 'text 50,800 "hello world"' f1.png f2.png

您可以使用
NSImage
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];


您可以使用
NSImage
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];


您忽略了将
f1
绘制到
f2
中。另外,使用老式的
lockFocus
,绘制,
unlockFocus
来构建
f2
,然后使用
data=[NSBitmapImageRep representationOfImageRepsInArray:f2.RepresentationusingType:NSPNGFileType属性:@{}]直接从其表示中创建数据可能是最简单的方法。此外,Y坐标的计算非常简单:
NSMaxY(dstRect)-800
。或者,您可以只使用翻转图像(将
YES
传递到代码中的翻转参数,或者根据我的建议使用
lockFocusFlipped:YES
)修复,谢谢。除了向后兼容之外,使用
-lockFocus
比使用更新的API有什么好处?我暂时将答案改回使用
-[NSBitmapImageRep initWithCGImage:
,因为我使用
-[NSBitmapImageRep representationOfImageRepsInArray:usingType:properties]测试代码
并导致
:ImageIO:CGImageDestinationFinalize image destination必须至少记录一个图像,并且返回
nil
。我用新的API和
-lockFocus
都试过了。是的,我只是做了同样的测试,发现了同样的东西。我建议使用
+imageWithSize:…
创建一个具有类
NSCustomImageRep
的单一表示的图像,我怀疑
+[NSBitmapImageRep representationOfImageRepsInArray:usingType:properties:][/code>是否能与这样的表示一起工作。我的期望是锁定焦点和绘图将创建一个可以工作的表示。但我们现在都发现,这是不正确的。很抱歉。您忽略了将
f1
绘制到
f2
中。另外,使用老式的
lockFocus
,绘制,
unlockFocus
来构建
f2
,然后使用
data=[NSBitmapImageRep representationOfImageRepsInArray:f2.RepresentationusingType:NSPNGFileType属性:@{}]直接从其表示中创建数据可能是最简单的方法。此外,Y坐标的计算非常简单:
NSMaxY(dstRect)-800
。或者,您可以只使用翻转图像(将
YES
传递到代码中的翻转参数,或者根据我的建议使用
lockFocusFlipped:YES
)修复,谢谢。除了向后兼容之外,使用
-lockFocus
比使用更新的API有什么好处?我暂时将答案改回使用
-[NSBitmapImageRep initWithCGImage:
,因为我使用
-[NSBitmapImageRep representationOfImageRepsInArray:usingType:properties]测试代码
并导致
:ImageIO:CGImageDestinationFinalize image destination必须至少记录一个图像,并且返回
nil
。我用新的API和
-lockFocus
都试过了。是的,我只是做了同样的测试,发现了同样的东西。我建议使用
+imageWithSize:…
创建一个具有类
NSCustomImageRep
的单一表示的图像,我怀疑
+[NSBitmapImageRep representationOfImageRepsInArray:usingType:properties:][/code>是否能与这样的表示一起工作。我的期望是锁定焦点和绘图将创建一个可以工作的表示。但我们现在都发现,这是不正确的。很抱歉。