In overriding a UIView drawRect, I draw the main image with CGContextDrawImage. Over it I need to draw another image (with a multiply blend mode), so I actually need to draw over it.
This second image needs to be prepares since it’s generated dynamically (it has to be masked, may be different size and so), so in the end I need to generate it. How can I get a second context where I can draw and mask this second image before applying it over the main one with? If I draw on the current context, it gets directly drawn no the main one before I can mask it.
- (void) drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextDrawImage(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height), [UIImage imageNamed:@"actionBg.png"].CGImage);
// prevent the drawings to be flipped
CGContextTranslateCTM(ctx, 0, rect.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
// generate the overlay
if ([self isActive] == NO && self.fullDelay != 0) { // TODO: remove fullDelay check!
int segmentSize = (ACTION_SIZE / [self fullDelay]);
for (int i=0; i<[self fullDelay]; i++) {
float alpha = 0.9 - (([self fullDelay] * 0.1) - (i * 0.1));
[[UIColor colorWithRed:120.0/255.0 green:14.0/255.0 blue:14.0/255.0 alpha:alpha] setFill];
if (currentDelay > i) {
CGRect r = CGRectMake(0, i * segmentSize, ACTION_SIZE, segmentSize);
CGContextFillRect(ctx, r);
}
[[UIColor colorWithRed:1 green:1 blue:1 alpha:0.3] setFill];
CGRect line = CGRectMake(0, (i * segmentSize) + segmentSize - 1 , ACTION_SIZE, 1);
CGContextFillRect(ctx, line);
}
UIImage *overlay = UIGraphicsGetImageFromCurrentImageContext();
UIImage *overlayMasked = [TDUtilities maskImage:overlay withMask:[UIImage imageNamed:@"actionMask.png"]];
}
}
Here overlayMasked now contains the correct image, but since I’ve prepared it using the main context, its is not all messed. Thanks
Thanks
You can create a bitmap context using either
UIGraphicsBeginImageContextWithOptionsorCGBitmapContextCreate. After you’re finished drawing in the bitmap context, you can get an image usingUIGraphicsGetImageFromCurrentImageContextorCGBitmapContextCreateImage(as appropriate), and then release the context using eitherUIGraphicsEndImageContextorCGContextRelease.