PDA

View Full Version : Image mask problem




mrl72
Jan 29, 2011, 12:50 PM
I've been using the same code to apply an image mask (non alpha channel PNG) to the image on a button without any problems. I've been doing this real-time but have recently decided to cache the image with the mask applied before I add it to the button. I have changed nothing except instead of applying the mask before it's added to the button, I write it to file and then use the cached version of the image on the button. No code changes to the mask process at all, which is as follows:



- (UIImage*) maskImage:(UIImage *)image large:(BOOL)largeImage {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

UIImage *maskImage;

if (largeImage) {
maskImage = [UIImage imageNamed:@"imagemask_large.png"];
} else {
maskImage = [UIImage imageNamed:@"imagemask.png"];
}

CGImageRef maskImageRef = [maskImage CGImage];

// create a bitmap graphics context the size of the image
CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);


if (mainViewContentContext==NULL)
return NULL;

CGFloat ratio = 0;

ratio = maskImage.size.width/ image.size.width;

if(ratio * image.size.height < maskImage.size.height) {
ratio = maskImage.size.height/ image.size.height;
}

CGRect rect1 = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
CGRect rect2 = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};


CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);


// Create CGImageRef of the main view bitmap content, and then
// release that bitmap context
CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);

UIImage *theImage = [UIImage imageWithCGImage:newImage];

CGImageRelease(newImage);

// return the image
return theImage;
}



Yet by doing this my images which were once perfect now have a weird transparency mask. Is it becuase the only difference is I'm writing the "masked" version out with UIImageJPEGRepresentation perhaps? Any ideas what's going wrong?

Example result before with setting the mask at run time and then applying to button (correct result - note the corners are black but they actually appear transparent when applied to the button):

http://loughers.smugmug.com/photos/1171131090_BcBQo-X3.jpg

Example after written to disk instead of applied to the button:

http://loughers.smugmug.com/photos/1171131080_CJUtp-X3.jpg



chown33
Jan 29, 2011, 01:13 PM
Is it becuase the only difference is I'm writing the "masked" version out with UIImageJPEGRepresentation perhaps? Any ideas what's going wrong?

JPEG/JFIF files don't have an alpha or masking channel.

http://en.wikipedia.org/wiki/JPEG_File_Interchange_Format
http://en.wikipedia.org/wiki/JPEG
Conspicuous by its absence is any mention of alpha, transparency, masking, etc.