0

I wish to overlay one CGImage over another.

As an example the first CGImage is 1024x768 and want to overlay a second 100x100 CGImage at a given location.

I have seen how to do this using NSImage but don't really want to convert my CGImages to NSImage's then do overlay then convert the result back to CGImage. I have also seen iOS versions of the code, but unsure how to go about it on Mac?

Equinox2000
  • 576
  • 6
  • 17
  • The iOS code should actually work on OS X, since the CoreGraphics APIs are identical on both platforms. – Lily Ballard Nov 06 '12 at 21:49
  • @KevinBallard Unfortunately the iOS example I found uses drawRect to render the overlaid image.. http://www.craftymind.com/2009/02/10/creating-the-loupe-or-magnifying-glass-effect-on-the-iphone/ not sure if I can get the context from first image and somehow do a CGContextDrawImage onto it? – Equinox2000 Nov 06 '12 at 22:14

1 Answers1

1

I'm mostly used to iOS, so I might be out of my depth here, but assuming you have a graphics context (sized like the larger of the two images), can't you just draw the two CGImages on top of each other?

CGImageRef img1024x768;
CGImageRef img100x100;

CGSize imgSize = CGSizeMake(CGImageGetWidth(img1024x768), CGImageGetHeight(img1024x768));

CGRect largeBounds = CGRectMake(0, 0, CGImageGetWidth(img1024x768), CGImageGetHeight(img1024x768));
CGContextDrawImage(ctx, largeBounds, img1024x768);

CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(img100x100), CGImageGetHeight(img100x100));
CGContextDrawImage(ctx, smallBounds, img100x100);

And then draw the result into a NSImage?

Erik Tjernlund
  • 1,963
  • 13
  • 12
  • You are correct this is the way to go about this, however i had a problem getting a context in first place. In the end i refactored my code to use NSImage – Equinox2000 Nov 07 '12 at 11:04