4

我在 iOS5 和 iOS6 上开发了一个应用程序。在我升级到 XCode 5 和 iOS7 之后,我有一些新的错误可以解决。

主要的是 colorMasking 不再起作用。完全相同的代码仍然可以在装有 iOS6 的手机上编译和运行。在 iOS7 上,蒙版颜色仍然存在。我试图在谷歌上找到答案,但没有找到答案。这是iOS7的错误,还是有人知道更好的颜色屏蔽方法?

这是代码:

- (UIImage*) processImage :(UIImage*) image
{
    UIImage *inputImage = [UIImage imageWithData:UIImageJPEGRepresentation(image, 1.0)];
    const float colorMasking[6]={100.0, 255.0, 0.0, 100.0, 100.0, 255.0};
    CGImageRef imageRef = CGImageCreateWithMaskingColors(inputImage.CGImage, colorMasking);
    UIImage* finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return finalImage;
}

这是我发现的几篇 StackOverflow 帖子,它们帮助我首先在 iOS6 中工作: 透明度 iOS iOS 颜色在 UIImage 中变为透明

4

2 回答 2

8

我偶然发现了一些CGImageCreateWithMaskingColorsUIImagePNGRepresentation. 这可能与您的问题有关,也可能无关。我发现如果:

  1. 如果使用CGImageCreateWithMaskingColors并立即将该图像添加到图像视图中,我可以看到透明度似乎已正确应用;

  2. 但在 iOS 7 中,如果我当时:

    • 从中获取此图像CGImageCreateWithMaskingColors并创建一个NSDatausing UIImagePNGRepresentation;和

    • 如果NSData使用重新加载图像imageWithData,则生成的图像将不再具有透明度。

    为了确认这一点,如果我writeToFile为此NSData在 Photoshop 等工具中检查保存的图像,我可以确认该文件没有应用任何透明度。

    这仅在 iOS 7 中表现出来。在 iOS 6 中它很好。

  3. 但是,如果我在步骤 1 中获取图像并通过它往返drawInRect,则保存图像并随后加载它的相同过程可以正常工作。

以下代码说明了这个问题:

- (UIImage*) processImage :(UIImage*) inputImage
{
    const float colorMasking[6] = {255.0, 255.0, 255.0, 255.0, 255.0, 255.0};
    CGImageRef imageRef = CGImageCreateWithMaskingColors(inputImage.CGImage, colorMasking);
    UIImage* finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    // If I put this image in an image view, I see the transparency fine.

    self.imageView.image = finalImage;                           // this works

    // But if I save it to disk and the file does _not_ have any transparency

    NSString *documentsPath           = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *pathWithoutTransparency = [documentsPath stringByAppendingPathComponent:@"image-but-no-transparency.png"];
    NSData   *data                    = UIImagePNGRepresentation(finalImage);
    [data writeToFile:pathWithoutTransparency atomically:YES];   // save it so I can check out the file in Photoshop

    // In iOS 7, the following imageview does not honor the transparency

    self.imageView2.image = [UIImage imageWithData:data];        // this does not work in iOS 7

    // but, if I round-trip the original image through `drawInRect` one final time,
    // the transparency works

    UIGraphicsBeginImageContextWithOptions(finalImage.size, NO, 1.0);
    [finalImage drawInRect:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
    UIImage *anotherRendition = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    data = UIImagePNGRepresentation(anotherRendition);
    NSString *pathWithTransparency = [documentsPath stringByAppendingPathComponent:@"image-with-transparancy.png"];
    [data writeToFile:pathWithTransparency atomically:YES];

    // But this image is fine

    self.imageView3.image = [UIImage imageWithContentsOfFile:pathWithTransparency];   // this does work

    return anotherRendition;
}
于 2013-10-01T07:55:38.057 回答
0

我正在加载一个 JPEG,它由于某种原因加载了一个 alpha 通道,这在屏蔽时不起作用,所以在这里我重新创建 CGImage 忽略 alpha 通道。可能有更好的方法来做到这一点,但这有效!

- (UIImage *)imageWithChromaKeyMasking {
    const CGFloat colorMasking[6]={255.0,255.0,255.0,255.0,255.0,255.0};
    CGImageRef oldImage = self.CGImage;
    CGBitmapInfo oldInfo = CGImageGetBitmapInfo(oldImage);
    CGBitmapInfo newInfo = (oldInfo & (UINT32_MAX ^ kCGBitmapAlphaInfoMask)) | kCGImageAlphaNoneSkipLast;
    CGDataProviderRef provider = CGImageGetDataProvider(oldImage);
    CGImageRef newImage = CGImageCreate(self.size.width, self.size.height, CGImageGetBitsPerComponent(oldImage), CGImageGetBitsPerPixel(oldImage), CGImageGetBytesPerRow(oldImage), CGImageGetColorSpace(oldImage), newInfo, provider, NULL, false, kCGRenderingIntentDefault);
    CGDataProviderRelease(provider); provider = NULL;
    CGImageRef im = CGImageCreateWithMaskingColors(newImage, colorMasking);
    UIImage *ret = [UIImage imageWithCGImage:im];
    CGImageRelease(im);
    return ret;
}
于 2014-10-28T22:40:07.637 回答