Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.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
Objective c 使用Cocoa/OSX合并/堆叠两个图像_Objective C_Macos_Cocoa - Fatal编程技术网

Objective c 使用Cocoa/OSX合并/堆叠两个图像

Objective c 使用Cocoa/OSX合并/堆叠两个图像,objective-c,macos,cocoa,Objective C,Macos,Cocoa,我有一个CGImageRef(我们称之为原始图像)和一个透明png(水印)。我正在尝试编写一种方法,将水印放置在原始图像的顶部,并返回一个CGImageRef 在iOS中,我会使用UIKit将它们都绘制到一个上下文中,但在OSX中这似乎是不可能的(不支持UIKit) 堆叠两个图像的最简单方法是什么?感谢提供了一个快速的“脏”解决方案,您可以使用NSImage绘图API: NSImage *background = [NSImage imageNamed:@"background"]; NSIma

我有一个CGImageRef(我们称之为原始图像)和一个透明png(水印)。我正在尝试编写一种方法,将水印放置在原始图像的顶部,并返回一个CGImageRef

在iOS中,我会使用UIKit将它们都绘制到一个上下文中,但在OSX中这似乎是不可能的(不支持UIKit)


堆叠两个图像的最简单方法是什么?感谢提供了一个快速的“脏”解决方案,您可以使用NSImage绘图API:

NSImage *background = [NSImage imageNamed:@"background"];
NSImage *overlay = [NSImage imageNamed:@"overlay"];

NSImage *newImage = [[NSImage alloc] initWithSize:[background size]];
[newImage lockFocus];

CGRect newImageRect = CGRectZero;
newImageRect.size = [newImage size];

[background drawInRect:newImageRect];
[overlay drawInRect:newImageRect];

[newImage unlockFocus];

CGImageRef newImageRef = [newImage CGImageForProposedRect:NULL context:nil hints:nil];

如果您不喜欢这样,那么您所期望的大多数CGContext API都可以跨平台进行绘制,并具有更多的控制。类似地,您可以查看NSGraphicsContext。

当您渲染到
CGContext时,这非常容易

如果希望生成图像,可以创建并渲染到
CGBitmapContext
,然后在渲染后请求图像

一般流程,省略了常见细节和上下文信息:

CGImageRef CreateCompositeOfImages(CGImageRef pBackground,
                                   const CGRect pBackgroundRect,
                                   CGImageRef pForeground,
                                   const CGRect pForegroundRect)
{
  // configure context parameters
  CGContextRef gtx = CGBitmapContextCreate( %%% );

  // configure context

  // configure context to render background image
  // draw background image
  CGContextDrawImage(gtx, pBackgroundRect, pBackground);

  // configure context to render foreground image
  // draw foreground image
  CGContextDrawImage(gtx, pForegroundRect, pForeground);

  // create result
  CGImageRef result = CGBitmapContextCreateImage(gtx);

  // cleanup

  return result;
}
您需要从PNG创建一个CGImage

您可能有兴趣使用的其他API:

  • CGContextSetBlendMode
  • CGContextSetAllowsAntialiasing
  • CGContextSetInterpolationQuality

我知道很多人通常会建议您使用更高级别的抽象(即AppKit和UIKit),但CoreGraphics是在这两种上下文中进行渲染的一个很好的库。如果您对易于在OS X和iOS中使用的图形实现感兴趣,如果您对这些抽象概念感到满意,CoreGraphics是一个很好的选择。

如果像我这样的人需要Swift版本

这是一个功能性Swift 5版本:

let background = NSImage(named: "background")
let overlay = NSImage(named: "overlay")

let newImage = NSImage(size: background.size)
newImage.lockFocus()

var newImageRect: CGRect = .zero
newImageRect.size = newImage.size

background.draw(in: newImageRect)
overlay.draw(in: newImageRect)

newImage.unlockFocus()

我希望有时间对CGContext示例做同样的操作。

注意,
drawInRect:
仅在10.9
[背景绘图点:(NSPoint){0,0}fromRect:newImageRect操作:NSCompositeSourceOver分数:1.0]中可用
适用于10.9之前版本,还需要注意的是,根据NSImage文档,“该方法使用NSCompositeSourceOver操作合成图像”。越低,效率越高,非常好!