views:

533

answers:

1

I have an image with something inside in a white backround. I want to save that image in a format that allows alpha channel or using an alpha mask in a way that the white pixels became transparents. Any light out there?

+2  A: 

I don't know of any libraries where this is super easy. But, there's a lot of relevant sample code in the GLImageProcessing example here. (I haven't run the following)

UIImage *some_image = [UIImage imageNamed:@"somethin'.tiff"];
CGImageRef cg_image = some_image.CGImage;
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(cg_image));
size_t bpp = CGImageGetBitsPerPixel(CGImage);

uint32_t *stuff = (uint32_t *)CFDataGetBytePtr(data);
int w = CGImageGetWidth(CGImage);
int h = CGImageGetHeight(CGImage);
int N = w * h;
for (int i = 0; i < N; i++ ) {
    // do your stuff, test for white, set the alpha mask
    stuff[i] = stuff[i] & ((uint32_t)0xFFFFFFFF | alpha_mask);
}

You could instead use this function

UIKIT_EXTERN NSData *UIImagePNGRepresentation(UIImage *image);                               

and write the data to disk. I hope this helps. Post the solution if you find it...

Robert Karl
Well i've been doing some research and in the File System, no matter what do you do with the image, the iPhone saves it as a .jpg image and it never saves alpha channel so far.Thanks anyway @robert.
sicario