在iOS中组合图像时损坏像素(Corrupt pixels when combining images in iOS)

我正在尝试将两张照片合并为一张图片(想想一张在另一个人脸上的照片顶部有一个洞的尸体)。 顶部图像有一些半透明像素和一些完全透明的像素,我想将它叠加在实心图像上。

这就是我正在做的事情:我有一个大小合适的Context,我在其上绘制底部图像,没有任何alpha(faceImage)。 最重要的是,我绘制了一个有透明孔的图像,具有不同级别的透明度(coverImage):

UIGraphicsBeginImageContext(view.bounds.size); [faceImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:1]; [coverImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:1]; UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

最终图像中的结果非常出乎意料:

顶部图像没有透明度的像素中,最终图像被正确保存并显示顶部图像中的像素。 ( 好的

顶部图像具有完全透明度的像素中,最终图像被正确保存并显示底部图像中的像素。 ( 好的

顶部图像具有半透明像素并且亮度非常轻 (最终像素的亮度)的像素中,我突然得到完全透明的像素 (而不是没有透明度的最终像素,其是顶部像素和底部之间的混合)像素)。 (WTF?)

在下面的图像中,您可以看到奇怪的白色斑点。 那些是完全透明像素的像素(通过它们看到白色背景):

这是我放在上面的图像:

这是底部的脸部图像:

可能导致这种情况的任何想法?

TIA

I'm trying to combine two photos into one image (think a body with a hole in the face on top of a picture of a different person's face). The top image has some semi-transparent pixels and some fully transparent pixels and I want to overlay it on top of a solid image.

Here's what I'm doing: I have a Context with the right size and I draw the bottom image on it, without any alpha (faceImage). On top of that I draw an image that has a transparent hole in it, with various levels of transparencies (coverImage):

UIGraphicsBeginImageContext(view.bounds.size); [faceImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:1]; [coverImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:1]; UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

The results in the final image are quite unexpected:

In pixels where the top image has no transparency the final image is saved properly and shows the pixel from the top image. (OK)

In pixels where the top image has full transparency the final image is saved properly and shows the pixel from the bottom image. (OK)

In pixels where the top image has semi-transparent pixels and the brightness is very light (the brightness of the final pixel) I suddenly get entirely transparent pixels (instead of a final pixel with no transparency that's a blend between the top pixel and the bottom pixel). (WTF?)

In the image below you can see weird blotches of white. Those are the pixels that became entirely transparent pixels (you see the white background through them):

This is the image that I'm putting on top:

This is the face image on the bottom:

Any ideas what could be causing this?

TIA

最满意答案

2个一般可能的线索:

您正在绘制的视图是不透明的吗? (http://stackoverflow.com/questions/1451977/transparent-color-works-on-the-simulator-but-becomes-black-on-the-iphone) 有关Alpha通道预乘的一些信息(http://iphonedevelopment.blogspot.com/2008/10/iphone-optimized-pngs.html)

干杯,

俄德。

We never really discovered what was causing the problem but did find out that when you change the brightness of the semi-transparent image in the application and save it, the problem occurs. If you preload an image that is bright the problem doesn't occur.

(Maybe it has to do with iPhone optimized PNGs, as described in Oded's link)

So as a work-around to solve the problem we just temporarily save the image after the brightness is changed and then use that saved image for the final blend. Here's the code we added:

NSData *coverImageData = UIImagePNGRepresentation(coverImage); coverImage = [UIImage imageWithData:coverImageData];

As you can see, we don't actually save the image as a file, it's enough to store it as a PNG representation and then load it back.

Complete hack, but it does the job.

更多推荐