5

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

this is what i have tried till now:

-(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 the the imageView3 i'm just showing it as i get it without background (till here i get it all fine) but when i convert to NSData an then get it back to UIImage it loses the transparency,

code running on iOS 7

enter image description here

Same code running on iOS 6 and below works perfect!!

enter image description here

i have created an example os my issue on Github example

fiigo0
  • 53
  • 1
  • 5
  • BTW, [responding to another question](http://stackoverflow.com/questions/19108457/cgimagecreatewithmaskingcolors-doesnt-work-with-ios7), I noticed that someone else was having a problem with `CGImageCreateWithMaskingColors` not preserving the alpha channel in iOS 7 in conjunction with `UIImagePNGRepresentation`. In my final solution below, I coincidentally removed the call to `CGImageCreateWithMaskingColors` as I streamlined your OpenGL code. Bottom line, I suspect you stumbled across an actual iOS 7 bug with `CGImageCreateWithMaskingColors` and it sounds like we've got a couple work-arounds. – Rob Oct 01 '13 at 16:14

1 Answers1

2

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

For example, using this image:

stroke

When I render that using your code, I get:

enter image description here

Perhaps there's something about the image that you started with.


The issue seems to stem from the OpenGLES code that created the image. As discussed here, it appears that you need to use

glClearColor(0.0, 0.0, 0.0, 0.0);

instead of

glClearColor(1.0, 1.0, 1.0, 0.0);

I also used the kCGImageAlphaPremultipliedLast value for CGBitmapInfo.

Doing all of that, rendered the call to CGImageCreateWithMaskingColors unnecessary, and the resulting image appears to be correctly preserves the alpha channel when you call UIImagePNGRepresentation.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Hi Rob, i have tried on the first UIImage view that i created imageView.backgroundColor = [UIColor clearColor]; and i just double check your answer adding the same backgroundColor for each uiimageview but no luck. Thank you for your response! – fiigo0 Sep 24 '13 at 14:32
  • im using https://github.com/jcmontiel/SignatureView/blob/master/SignatureView.m this library to generate the image on the method "- (UIImage *) getSignatureImage", i have check the code with your image and its working fine, but for some reason with the image generated it does not. – fiigo0 Sep 24 '13 at 15:32
  • @fiigo0 I'm not an OpenGLES guy, so I'm not sure why that's not rendering the transparent background correctly. But the line `eaglLayer.opaque = YES;` looks highly suspect. I'd suggest trying `NO`, which is the typical answer for OpenGLES background transparency. See [this answer](http://stackoverflow.com/a/4456862/1271826). – Rob Sep 24 '13 at 16:29
  • As an aside, I did a quick test creating a path using CoreGraphics' `CGContextDrawPath(context, kCGPathStroke);` and the resulting image had the correct transparent background. I'm sure if you used a `CAShapeLayer`, that would work, too. (BTW, both CoreGraphics and `CAShapeLayer` strike me as easier ways to draw simple line paths.) – Rob Sep 24 '13 at 16:31
  • 1
    i have tried eaglLayer.opaque = NO i forgot to mention that change that solved transparency for ios6, ill take a look at CoreGraphics as you mention, i really appreciate your time and your help! – fiigo0 Sep 24 '13 at 22:54
  • Hi again Rob, about what you mention to get the transparent background on iOS 6 i had to set eaglLayer.opaque = NO also create the image with a mask on the SignatureView.m on method getSignatureImage. – fiigo0 Sep 25 '13 at 16:43
  • Hi Rob, the issue is SOLVED! form some reason on iOS7 the value of the bitsPerComponent has to be 1 (not sure why) so adding the following code on getSignatureImage method solved the transparent issue on iOS7 int bitsPerComponent; // prep the ingredients if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { bitsPerComponent = 1; } else { bitsPerComponent = 8; } – fiigo0 Sep 25 '13 at 16:45
  • @fiigo0 That's great if that works (though, I confess, it seems strange). I also sent you a pull request that includes another solution, that I came up with. – Rob Sep 25 '13 at 16:49
  • yes i just saw your mail i'll check your code! thank you for your time and patience! – fiigo0 Sep 25 '13 at 17:46