gpt4 book ai didi

ios - CGImageCreateWithMaskingColors 不适用于 iOS7

转载 作者:可可西里 更新时间:2023-11-01 05:13:01 26 4
gpt4 key购买 nike

我在 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 中使用它: Transparency iOS iOS color to transparent in UIImage

最佳答案

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

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

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

    • CGImageCreateWithMaskingColors 中获取此图像并使用 UIImagePNGRepresentation 创建一个 NSData;和

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

    为了确认这一点,如果我为此 NSData writeToFile 并在 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;
}

关于ios - CGImageCreateWithMaskingColors 不适用于 iOS7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19108457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com