开发者

Corrupt pixels when combining images in iOS

开发者 https://www.devze.com 2023-04-01 11:56 出处:网络
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).

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();
UIGraphicsEndI开发者_如何学CmageContext();

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):

Corrupt pixels when combining images in iOS

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

Corrupt pixels when combining images in iOS

This is the face image on the bottom:

Corrupt pixels when combining images in iOS

Any ideas what could be causing this?

TIA


2 general possible leads:

  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)
  2. Something about the alpha channel being pre-multiplied (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)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消