【问题标题】:XCODE 5 iOS7 how to convert UIImage (PNG) to NSData without losing the transparent backgroundXCODE 5 iOS7 如何在不丢失透明背景的情况下将 UIImage (PNG) 转换为 NSData
【发布时间】:2013-09-29 18:44:13
【问题描述】:

我有一个接收UIImage 的方法,我将其转换为NSData 并请求发布该数据,它适用于iOS 6,但当我尝试使用iOS 7 时,图像失去透明背景。

这是我到目前为止所尝试的:

-(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];

imageView3 上,我只是在没有背景的情况下显示它(直到这里我一切都很好)但是当我转换为NSData 然后将其恢复为UIImage 它失去了透明度,

在 iOS 7 上运行的代码

在 iOS 6 及更低版本上运行的相同代码完美无缺!!

我在Github example创建了一个示例操作系统我的问题

【问题讨论】:

  • 顺便说一句,responding to another question,我注意到其他人遇到了CGImageCreateWithMaskingColors 没有在 iOS 7 中与UIImagePNGRepresentation 一起保留 alpha 通道的问题。在下面的最终解决方案中,我巧合地删除了对 CGImageCreateWithMaskingColors 的调用,因为我简化了您的 OpenGL 代码。归根结底,我怀疑你偶然发现了一个带有 CGImageCreateWithMaskingColors 的实际 iOS 7 错误,听起来我们有一些变通办法。

标签: objective-c uiimage ios7 uiimagepngrepresentation


【解决方案1】:

当我在 iOS7 上对具有透明度的图像使用 UIImagePNGRepresentation 时,会保留透明度。

例如,使用这张图片:

当我使用你的代码渲染它时,我得到:

也许你开始使用的图像有一些东西。


问题似乎源于创建图像的 OpenGLES 代码。正如here 所讨论的,您似乎需要使用

glClearColor(0.0, 0.0, 0.0, 0.0);

而不是

glClearColor(1.0, 1.0, 1.0, 0.0);

我还为CGBitmapInfo 使用了kCGImageAlphaPremultipliedLast 值。

执行所有这些操作后,对 CGImageCreateWithMaskingColors 的调用就变得不必要了,并且当您调用 UIImagePNGRepresentation 时,生成的图像似乎正确地保留了 alpha 通道。

【讨论】:

  • 嗨 Rob,我已经尝试过创建的第一个 UIImage 视图 imageView.backgroundColor = [UIColor clearColor];我只是仔细检查你的答案,为每个 uiimageview 添加相同的背景颜色,但没有运气。感谢您的回复!
  • 我使用github.com/jcmontiel/SignatureView/blob/master/SignatureView.m这个库在方法“- (UIImage *) getSignatureImage”上生成图像,我已经检查了你的图像的代码并且它工作正常,但由于某种原因图像生成它没有。
  • @fiigo0 我不是 OpenGLES 人,所以我不确定为什么不能正确渲染透明背景。但是eaglLayer.opaque = YES; 这条线看起来很可疑。我建议尝试NO,这是 OpenGLES 背景透明度的典型答案。见this answer
  • 顺便说一句,我做了一个快速测试,使用 CoreGraphics 的CGContextDrawPath(context, kCGPathStroke); 创建路径,结果图像具有正确的透明背景。我敢肯定,如果您使用CAShapeLayer,那也可以。 (顺便说一句,CoreGraphics 和 CAShapeLayer 都让我觉得绘制简单线条路径的方法更简单。)
  • 我已经尝试过 eaglLayer.opaque = 不,我忘了提到解决 ios6 透明度问题的更改,我看看你提到的 CoreGraphics,非常感谢你的时间和帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
  • 2012-03-12
相关资源
最近更新 更多