Question

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?

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top