如何将多个 NSImage 合成为一张大图像?
我有一个对象集合,描述图像名称、其大小及其 X/Y 位置。该集合按“层”排序,因此我可以用某种画家的算法合成图像。
由此,我可以确定保存所有图像所需的矩形,所以现在我想做的是:
- 创建某种缓冲区来保存结果(NS 相当于 iPhoneOS 调用的 UIGraphicsContext。)
- 将所有图像绘制到缓冲区。
- 从缓冲区的合成结果中获取一个新的 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();
我正在寻找的是如何在 Desktop Cocoa/NS 中执行此操作。
谢谢!
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:
- Create some sort of buffer to hold the result (The NS equivalent of what iPhoneOS calls UIGraphicsContext.)
- Draw all the images into the buffer.
- Snag a new NSImage out of the composited result of the buffer.
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();
What I'm looking for is how to do that in Desktop Cocoa/NS.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查 Apple 绘图指南 提供更长、更详细的答案。
Check Apple's Drawing Guide for a much longer, more detailed answer.