Note that there are some explanatory texts on larger screens.

plurals
  1. POAny idea why this image masking code does not work?
    primarykey
    data
    text
    <p>I have this code to mask an image. Basically, I only work with PNG images. So I have a 300x400 PNG image with 24bits of color (PNG-24). I am not sure if it also has an alpha channel. But there's no transparency in it.</p> <p>Then, there is the image mask which is PNG-8bit without alpha channel. It is just black, grayscale and white.</p> <p>I create both images as UIImage. Both display correctly when putting them into an UIImageView. </p> <p>Then I create an UIImage out of them which contains the results of the masking operation, with this code:</p> <pre><code>+ (UIImage*)maskImage:(UIImage*)image withMask:(UIImage*)maskImage { CGImageRef maskRef = maskImage.CGImage; CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), CGImageGetHeight(maskRef), CGImageGetBitsPerComponent(maskRef), CGImageGetBitsPerPixel(maskRef), CGImageGetBytesPerRow(maskRef), CGImageGetDataProvider(maskRef), NULL, false); CGImageRef masked = CGImageCreateWithMask([image CGImage], mask); return [UIImage imageWithCGImage:masked]; } </code></pre> <p>here's what I do with that:</p> <pre><code> UIImage *image = [UIImage imageNamed:@"coloredImagePNG24.png"]; UIImage *maskImage = [UIImage imageNamed:@"theMaskPNG8_Grayscale_NoAlpha.png"]; UIImage *maskedImage = [MyGraphicUtils maskImage:image withMask:maskImage]; UIImageView *testImageView = [[UIImageView alloc] initWithImage:maskedImage]; testImageView.backgroundColor = [UIColor clearColor]; testImageView.opaque = NO; </code></pre> <p>After all that, the coloredImagePNG24.png stays totally intact as it is. No masking is happening. But now the weird thing is: If I turn that around, i.e. use this image as the mask, and the mask as the color-image-to-mask, then I get something very ugly in grayscale (but masked ;) ).</p> <p>Any idea what's wrong with my code?</p> <p>UPDATE: I just googled for an different b/w png to use it as a mask. And then this one worked! But the one I made by myself does not work. So I assume that the code has big image decoding problems. I would have to "normalize" the images to a specific format, so that it works.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload