简体   繁体   English

在iOS中组合图像时损坏像素

[英]Corrupt pixels when combining images in iOS

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). 这就是我正在做的事情:我有一个大小合适的Context,我在其上绘制底部图像,没有任何alpha(faceImage)。 On top of that I draw an image that has a transparent hole in it, with various levels of transparencies (coverImage): 最重要的是,我绘制了一个有透明孔的图像,具有不同级别的透明度(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?) (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 TIA

2 general possible leads: 2个一般可能的线索:

  1. Is the view you're drawing to opaque? 您正在绘制的视图是不透明的吗? (http://stackoverflow.com/questions/1451977/transparent-color-works-on-the-simulator-but-becomes-black-on-the-iphone) (http://stackoverflow.com/questions/1451977/transparent-color-works-on-the-simulator-but-becomes-black-on-the-iphone)
  2. Something about the alpha channel being pre-multiplied (http://iphonedevelopment.blogspot.com/2008/10/iphone-optimized-pngs.html) 有关Alpha通道预乘的一些信息(http://iphonedevelopment.blogspot.com/2008/10/iphone-optimized-pngs.html)

Cheers, 干杯,

Oded. 俄德。

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) (也许它与iPhone优化的PNG有关,如Oded的链接中所述)

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. 正如您所看到的,我们实际上并不将图像保存为文件,只需将其存储为PNG表示,然后将其加载回来即可。

Complete hack, but it does the job. 完全破解,但它完成了工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM