user339946
user339946

Reputation: 6119

How to layer on the CGContext

I'm using a bit of a workaround in order to successfully save an image with a mask (because you can't render masks with CoreAction).

Here's my code:

CGContextRef context = CGBitmapContextCreate(nil, cWidth, cHeight, bitsPerComponent, bytesPerRow, CGColorSpaceCreateDeviceRGB(), CGImageGetBitmapInfo(image));
//the mask:
CGContextClipToMask(context, CGRectMake(0,0,1280,935), self.image.image.CGImage);
//the image to mask:
CGContextDrawImage(context, CGRectMake(0, 0, 1280,935), viewImage.CGImage);

CGImageRef mergeResult  = CGBitmapContextCreateImage(context);
    saver = [[UIImage alloc] initWithCGImage:mergeResult];

So this works pretty well, the mask cuts out everything outside of its shape in the target image. This makes the surrounding area and background white. Rather than show white, I would like to show an image/color/pattern, etc. So I'd basically like to stack another image behind all that.

How can this be done? Thanks

Upvotes: 0

Views: 898

Answers (1)

Kurt Revis
Kurt Revis

Reputation: 27984

Draw your background into the context first, then clip, then draw your image into the same context.

Upvotes: 1

Related Questions