views:

838

answers:

1

I need to draw a series of PNGs into a CG context so I can blend them together:

CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetBlendMode (context, kCGBlendModeOverlay);

Currently I have my app working based on just drawing each image as a UIImageView and adding them as a subview to the view file via: [self addSubview:someImageView] ...but this doesn't allow for blending mode manipulation, right?

Currently the UIImageView vars are being assigned via: someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image-name.png"]];

So I've tried replacing those with either of these with no luck: [[UIImage imageNamed:@"image-name.png"] drawInRect: rect blendMode:kCGBlendModeOverlay alpha:1.0];

and this one needs a CGImage which I'm not sure how to make with the PNGs: CGContextDrawImage(context, rect, someCGImage);

So what am I missing? These aren't being triggered by any interaction, they just need to load/draw when the app loads.

Thanks for any leads. :)

+1  A: 

You don't need to be using UIImageViews. Instead, bracket your drawing between calls to UIGraphicsBeginImageContext and UIGraphicsEndImageContext.

For example (code not tried):

UIImage* uiimage = [UIImage imageNamed:@"image-name.png"];
UIImage* uiimage2 = [UIImage imageNamed:@"other-image-name.png"];

CGSize size = uiimage.size; 

UIGraphicsBeginImageContext(size);

[uiimage drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0];
[uiimage2 drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0];

UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

If you want to use CG calls, call:

CGContextRef context = UIGraphicsGetCurrentContext();

to get the context ref.

Ken Aspeslagh
BTW, it's easy to make a CGImage from a UIImage. There is CGImage property on UIImage. However, avoiding mixing UIImage and CGImage drawing routines, or else you'll find that the drawing context is upside-down.
Ken Aspeslagh
I wasn't clear on that last part, as I want to rotate the uiimages before drawing them, but calling CGContextRotateCTM(context, 3.14); before the drawing command didn't actually rotate by the desired 180 degrees.And yes, the first command I called in the drawRect method was CGContextRef context = UIGraphicsGetCurrentContext();Thoughts?
steganous
It sounds like you're doing it right. Can you post your new code?
Ken Aspeslagh