如何将多个NSImages合成为一个大图像? [英] How to composite several NSImages into one big image?

查看:80
本文介绍了如何将多个NSImages合成为一个大图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组对象,这些对象描述了图像名称,尺寸和X / Y位置。该集合按图层排序,因此我可以使用某种画家的算法对这些图像进行合成。

I have a collection of objects which describe an image-name, its size and it's X/Y location. The collection is sorted by "layers", so I can composite the images in a sort of painter's algorithm.

由此,我可以确定容纳所有图像的矩形图像,所以现在我要做的是:

From this, I can determine the rectangle necessary to hold all of the images, so now what I want to do is:


  • 创建某种缓冲区来保存结果(与iPhoneOS的NS等效)调用UIGraphicsContext。)

  • 将所有图像绘制到缓冲区中。

  • 从缓冲区的合成结果中获取新的NSImage。

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

In iPhoneOS, this is the code that does what I want:

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();

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

What I'm looking for is how to do that in Desktop Cocoa/NS.

谢谢!

推荐答案

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];

检查 Apple的绘图指南,给出了更长,更详细的答案。

Check Apple's Drawing Guide for a much longer, more detailed answer.

这篇关于如何将多个NSImages合成为一个大图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆