CGImageCreateWithMaskingColors不适用于iOS7 [英] CGImageCreateWithMaskingColors Doesn't Work with iOS7
问题描述
我在iOS5和iOS6上开发了一款应用。在我升级到XCode 5和iOS7之后,我有一些新的bug。
I've developed an app on iOS5 and iOS6. After I upgraded to XCode 5 and iOS7, I have some new bugs to play with.
主要的是colorMasking不再有效。完全相同的代码仍可编译并适用于iOS6手机。在iOS7上,蒙面颜色仍然存在。我试图在谷歌找到答案,但还没有找到答案。这是iOS7的错误,还是有人知道更好的做彩色掩码的方法?
The main one is the colorMasking no longer works. The exact same code still compiles and works on a phone with iOS6. On iOS7, the masked color is still there. I tried to find the answer on Google, but haven't found an answer. Is it a bug of iOS7, or does anybody know of a better way of doing colormasking?
以下是代码:
- (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中透明
Here are a couple StackOverflow posts I found that helped me get it working in iOS6 the first place: Transparency iOS iOS color to transparent in UIImage
推荐答案
我偶然发现 CGImageCreateWithMaskingColors
与 UIImagePNGRepresentation
一起的奇怪行为。这可能与您的问题有关,也可能与您无关。我发现如果:
I have stumbled across some strange behavior of CGImageCreateWithMaskingColors
in conjunction with UIImagePNGRepresentation
. This may or may not be related to your problem. I have found that if:
-
如果使用
CGImageCreateWithMaskingColors
并立即添加该图像到图像视图,我可以看到透明度似乎已正确应用;
If use
CGImageCreateWithMaskingColors
and immediately add that image to an image view, I can see that the transparency appears to have been applied correctly;
但在iOS 7中,如果我那么:
But in iOS 7, if I then:
-
从
CGImageCreateWithMaskingColors
中获取此图像并创建NSData
使用UIImagePNGRepresentation
;和
take this image from
CGImageCreateWithMaskingColors
and create aNSData
usingUIImagePNGRepresentation
; and
如果使用 imageWithData $从
NSData
重新加载图像c $ c>,然后生成的图像将不再具有透明度。
if reload the image from that NSData
using imageWithData
, then the resulting image will no longer have its transparency.
要确认这一点,如果我<这个 NSData
的code> writeToFile 并在像Photoshop这样的工具中检查保存的图像,我可以确认该文件没有任何应用透明度。
To confirm this, if I writeToFile
for this NSData
and examine the saved image in a tool like Photoshop, I can confirm that the file does not have any transparency applied.
这仅在iOS 7中显现。在iOS 6中没关系。
This only manifests itself in iOS 7. In iOS 6 it's fine.
但是,如果我在步骤1中拍摄图像并通过 drawInRect
进行往返,则保存图像并随后加载图像的过程相同。
But if I take the image in step 1 and roundtrip it through drawInRect
, the same process of saving the image and subsequently loading it works fine.
以下代码说明了问题:
- (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;
}
这篇关于CGImageCreateWithMaskingColors不适用于iOS7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!