gpt4 book ai didi

cocoa - 用新鲜的背景合成图像?

转载 作者:行者123 更新时间:2023-12-03 16:43:36 25 4
gpt4 key购买 nike

我正在尝试将一个图像绘制到另一个图像上,然后将该合成图像输出为图像文件。该方法在我第一次调用它时工作正常,但任何后续调用都会在最后一个合成上叠加另一个图像。 IE。它不断分层而不是获取新的背景图像。

我希望我已经说清楚了(有点难以解释)任何帮助将不胜感激。

-(NSImage *)compositeImage:(NSImage *)overlay Onto:(NSImage *)background AtPoint:(NSPoint)location{
NSImage *returnImage;

[background lockFocus];

[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[overlay drawInRect:NSMakeRect(location.x, location.y, [overlay size].width, [overlay size].width) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

[background unlockFocus];

returnImage = background;

return returnImage;

}

最佳答案

您应该在绘制图像之前复制图像,这样就不会更改原始图像。

-(NSImage *)compositeImage:(NSImage *)overlay Onto:(NSImage *)background AtPoint:(NSPoint)location{
NSImage * backgroundCopy = [background copy];
[backgroundCopy lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[overlay drawInRect:NSMakeRect(location.x, location.y, [overlay size].width, [overlay size].width) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[backgroundCopy unlockFocus];
return backgroundCopy; //release this object in calling function.
}

关于cocoa - 用新鲜的背景合成图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9159931/

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