如何将几个NSImages合成一个大图像?

Oli*_*lie 15 cocoa image-manipulation nsimage

我有一组描述图像名称,大小和X/Y位置的对象.该集合按"图层"排序,因此我可以用一种画家的算法合成图像​​.

由此,我可以确定保存所有图像所需的矩形,所以现在我想要做的是:

  • 创建一些缓冲区来保存结果(相当于iPhoneOS调用UIGraphicsContext的NS.)
  • 将所有图像绘制到缓冲区中.
  • 从缓冲区的合成结果中获取新的NSImage.

在iPhoneOS中,这是执行我想要的代码:

UIGraphicsBeginImageContext (woSize);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] set];
    CGContextFillRect(ctx, NSMakeRect(0, 0, woSize.width, woSize.height));
    // draw my various images, here.
    // i.e. Various repetitions of [myImage drawAtPoint:somePoint];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

我正在寻找的是如何在Desktop Cocoa/NS中做到这一点.

谢谢!

and*_*n22 19

NSImage* resultImage = [[[NSImage alloc] initWithSize:imageSize] autorelease];
[resultImage lockFocus];

[anotherImage drawAtPoint:aPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
// Or any of the other about 6 options; see Apple's guide to pick.

[resultImage unlockFocus];
Run Code Online (Sandbox Code Playgroud)

查看Apple的绘图指南,获取更长,更详细的答案.