Writing a masked image to disk as a PNG file

后端 未结 2 1627
太阳男子
太阳男子 2021-02-20 01:15

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 poi

相关标签:
2条回答
  • 2021-02-20 01:52

    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)

    0 讨论(0)
  • 2021-02-20 02:06

    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.
    0 讨论(0)
提交回复
热议问题