Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a masked image to disk as a PNG file

Basically I'm downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I'm using the masking code everyone seems to point at which can be found here: http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html

What happens though, is that the image displays fine, but the version that gets written to the disk with

UIImage *img = [self maskImage:[UIImage imageWithData:data] withMask:self.imageMask];
[UIImagePNGRepresentation(img) writeToFile:cachePath atomically:NO];

has it's alpha channel inverted when compared to the one displayed later on (using the same UIImage instance here).

Any ideas? I do need the cached version to be masked, otherwise displaying the images in a table view get's awfully slow if I have to mask them every time.

Edit: So yeah, UIImagePNGRepresentation(img) seems to invert the alpha channel, doesn't have anything to do with the code that writes to disk, which is rather obvious but I checked anyway.

like image 645
Joonas Trussmann Avatar asked Nov 10 '09 14:11

Joonas Trussmann


2 Answers

How about drawing into a new image, and then save that?

UIGraphicsBeginImageContext(img.size);
[img drawAtPoint:CGPointZero];
UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[UIImagePNGRepresentation(newImg) writeToFile:cachePath atomically:NO];

(untested)

like image 62
epatel Avatar answered Oct 23 '22 06:10

epatel


See the description in CGImageCreateWithMask in CGImage Reference:

The resulting image depends on whether the mask parameter is an image mask or an image. If the mask parameter is an image mask, then the source samples of the image mask act as an inverse alpha value. That is, if the value of a source sample in the image mask is S, then the corresponding region in image is blended with the destination using an alpha value of (1-S). For example, if S is 1, then the region is not painted, while if S is 0, the region is fully painted.

If the mask parameter is an image, then it serves as an alpha mask for blending the image onto the destination. The source samples of mask' act as an alpha value. If the value of the source sample in mask is S, then the corresponding region in image is blended with the destination with an alpha of S. For example, if S is 0, then the region is not painted, while if S is 1, the region is fully painted.

It seems for some reason the image mask is treated as a mask image to mask with while saving. According to:

  • UIImagePNGRepresentation and masked images
  • http://lists.apple.com/archives/quartz-dev/2010/Sep/msg00038.html

to correctly save with UIImagePNGRepresentation, there are several choices:

  1. Use inverse version of the image mask.
  2. Use "mask image" instead of "image mask".
  3. Render to a bitmap context and then save it, like epatel mentioned.
like image 45
Hailei Avatar answered Oct 23 '22 05:10

Hailei