Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to composite several NSImages into one big image?

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!

like image 413
Olie Avatar asked Mar 04 '10 00:03

Olie


People also ask

How do I combine multiple pictures into one picture?

Open the Photo Gallery and locate the folder that contains photos you want to combine. Hold CTRL key to select multiple images and then click on the Photo Gallery's Create tab. Select the Photo Fuse feature and proceed to designate the area of the photo you want to replace.

How do you blend multiple images together?

To merge multiple images, use the Layer Mask, Blend Mode, or Layer Opacity tools to control the merge function. The Layer Opacity tool is the simplest method to merge multiple images.

How can I blend multiple images into a single panorama image?

Open your images in Photoshop and select File – Automate – Photomerge. Photoshop will give you some options. I usually start with the Auto layout option and ask Photoshop to blend the images. Once Photoshop creates the panoramic picture, I merge the layers.


1 Answers

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

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

like image 78
andyvn22 Avatar answered Sep 23 '22 05:09

andyvn22