XCODE 5 iOS7 how to convert UIImage (PNG) to NSData without losing transparent background

I have a method that receives UIImage , I convert it to NSData and make a request to publish Data, it works on iOS 6, but when I try to use iOS 7, the image loses a transparent background.

this is what i have tried so far:

 -(void)post:(UIImage *)firm name: { int x = 350; NSData *imageData = UIImagePNGRepresentation(firm); UIImage *image=[UIImage imageWithData:imageData]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, x, 40, 50)]; imageView.backgroundColor = [UIColor clearColor]; imageView.image = image; NSData *imageData2 = [NSData dataWithData:UIImagePNGRepresentation(firm)]; UIImage *image2=[UIImage imageWithData:imageData2]; UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(160, x, 40, 50)]; imageView2.image = image2; UIImageView *imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(110, x, 40, 50)]; imageView3.image = firm; UIImage * img = [UIImage imageWithData:UIImagePNGRepresentation(image)]; UIImageView *imageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(210, x, 40, 50)]; imageView4.image = img; [self.view addSubview:imageView]; [self.view addSubview:imageView2]; [self.view addSubview:imageView3]; [self.view addSubview:imageView4]; 

on imageView3 I just show it as I get it without background (until I get everything ok), but when I convert to NSData and then put it back in UIImage , it loses transparency.

code works on iOS 7

enter image description here

The same code that runs on iOS 6 and below works great!

enter image description here

I created an example os of my problem on a Github example

+4
source share
1 answer

When I use UIImagePNGRepresentation on an image with transparency on iOS7, the transparency is preserved.

For example, using this image:

stroke

When I do this using your code, I get:

enter image description here

Maybe something about the image you started with.


The problem seems to be related to the OpenGLES code that created the image. As discussed here , it seems that you need to use

 glClearColor(0.0, 0.0, 0.0, 0.0); 

instead

 glClearColor(1.0, 1.0, 1.0, 0.0); 

I also used the kCGImageAlphaPremultipliedLast value for CGBitmapInfo .

By doing all this, an unnecessary call to CGImageCreateWithMaskingColors , and the resulting image will correctly save the alpha channel when calling UIImagePNGRepresentation .

+2
source

All Articles