English 中文(简体)
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.

最佳回答

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:

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.
问题回答

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)





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

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 ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签