Search code examples
iphone-sdk-3.0uiimageviewuiimagealphaalphablending

Preserving alpha issues with loaded UIImages - how to avoid discarding alpha values on import?


My application lets the user save/load images with alpha values to the camera roll on the device. I can see in the Photos application that these images are appropriately alpha'd, as alpha'd areas just appear black. However, when I load these images back into the application using the valueForKey:UIImagePickerControllerOriginalImage message to the info dictionary from (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info, the alpha values are turned to just white, making those sections opaque. Is there a way to preserve these alpha values?


Solution

  • Found it out! Here's my function for doing this. Your milage may vary!:

    - (UIImage*)processImage:(UIImage*)image
    {
        CGImageRef reference = image.CGImage;
        float colors[6] = {0xEE, 0xFF, 0xEE, 0xFF, 0xEE, 0xFF};
        CGImageRef newimage = CGImageCreateWithMaskingColors(reference, colors);
        UIImage *result = [[UIImage alloc] initWithCGImage:newimage];
        CGImageRelease(newimage);
        return result;
    }