Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a CGImageRef from Context-Drawn Images?

Ok using coregraphics, I'm building up an image which will later be used in a CGContextClipToMask operation. It looks something like the following:

    UIImage *eyes = [UIImage imageNamed:@"eyes"];
    UIImage *mouth = [UIImage imageNamed:@"mouth"];

    UIGraphicsBeginImageContext(CGSizeMake(150, 150));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 0, 0, 0, 1);
    CGContextFillRect(context, bounds);
    [eyes drawInRect:bounds blendMode:kCGBlendModeMultiply alpha:1];
    [mouth drawInRect:bounds blendMode:kCGBlendModeMultiply alpha:1];

    // how can i now get a CGImageRef here to use in a masking operation?

    UIGraphicsEndImageContext();

Now, as you can see by the comment, I'm wondering how I'm actually going to USE the image I've built up. The reason why I'm using core graphics here and not just building up a UIImage is that the transparency I'm creating is very important. If I just grab a UIImage from the context, when it's used as a mask, it will just apply to everything... Further to the point, will I have any problems using a partially-transparent mask using this method?

like image 835
NonatomicRetain Avatar asked Dec 26 '11 16:12

NonatomicRetain


2 Answers

CGImageRef result = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
like image 128
Nico Avatar answered Sep 25 '22 21:09

Nico


You can call the UIGraphicsGetImageFromCurrentImageContext function, which will return a UIImage object. You can hold onto and use the UIImage, or ask it for its CGImage.

like image 43
Peter Hosey Avatar answered Sep 24 '22 21:09

Peter Hosey