Drawing a PNG image in a graphics context for manipulating the blend mode

I need to draw a PNG series in a CGContext so that I can combine them:

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

Currently I have an application based on the simple drawing of each image as a UIImageView and adding them as a subtask to the view file with: [self addSubview:someImageView] ... but this does not allow you to manipulate the blend mode, right?

Currently, UIImageView vars are assigned through:

 someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image-name.png"]]; 

So, I tried replacing them with any of them with no luck:

 [[UIImage imageNamed:@"image-name.png"] drawInRect:rect blendMode:kCGBlendModeOverlay alpha:1.0]; 

and for this I need CGImage , which I'm not sure how to do using PNG:

 CGContextDrawImage(context, rect, someCGImage); 

So what am I missing? They are not triggered by any interaction, they just need to load / draw when the application loads.

Thanks for any conclusions. :)

+7
iphone cocoa-touch core-graphics uiimage quartz-graphics
source share
1 answer

You do not need to use UIImageViews. Instead, copy the picture between calls to UIGraphicsBeginImageContext and UIGraphicsEndImageContext .

For example (code not verified):

 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.

+19
source share

All Articles