CGBitMapContextCreate 方法导致编译器警告 Xcode 5 而不是 Xcode 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18831179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:48:25  来源:igfitidea点击:

CGBitMapContextCreate Method Causes Compiler Warning Xcode 5 not Xcode 4

iosxcodecgcontextxcode5cgbitmapcontextcreate

提问by jac300

I just updated Xcode from version 4.6.2 to 5.0, and after doing a method in my project (created in Xcode 4.6.2) is suddenly giving a compiler warning. I have tried re-opening the project in both the old and new versions of Xcode, and I have confirmed that the same method gives no warnings in 4.6.2.

我刚刚将 Xcode 从版本 4.6.2 更新到 5.0,在我的项目(在 Xcode 4.6.2 中创建)中执行一个方法后,突然发出编译器警告。我已经尝试在旧版本和新版本的 Xcode 中重新打开该项目,并且我已经确认相同的方法在 4.6.2 中没有给出警告。

Here is the line of code eliciting the warning in Xcode 5.0:

这是在 Xcode 5.0 中引发警告的代码行:

CGContextRef context = CGBitmapContextCreate(NULL, frame.size.width * scaleFactor, frame.size.height * scaleFactor, 8, frame.size.width * scaleFactor * 4, colorSpace, kCGImageAlphaPremultipliedFirst);

And the warning says:

警告说:

"Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGBitMapInfo' (aka 'enum CGBitMapInfo')"

It does not appear to be a deprecation warning, but I am not quite familiar enough with these classes to interpret the meaning or know how to resolve it. Any help is appreciated.

它似乎不是弃用警告,但我对这些类不太熟悉,无法解释含义或知道如何解决它。任何帮助表示赞赏。

回答by nevyn

The kCGImageAlpha*enum values are supposed to fill the first five bits in CGBitmapInfo. However, since the C type system can't express this, you get a warning that the types don't match, even though they were intended to.

kCGImageAlpha*枚举值应该在填补了前五位CGBitmapInfo。但是,由于 C 类型系统无法表达这一点,您会收到类型不匹配的警告,即使它们是有意为之。

The correct solution is to cast your alpha enum value to CGBitmapInfo, since that's what it is:

正确的解决方案是将您的 alpha 枚举值转换为CGBitmapInfo,因为它是这样的:

(CGBitmapInfo)kCGImageAlphaPremultipliedFirst

回答by CodeBrew

Saw a comment https://github.com/inkling/Subliminal/issues/23by aegolden that the intention of the new XCode warning might be directing you to use different masks on these enum types to construct and concatenate various flags. So instead of just using kCGImageAlphaPremultipliedFirst, use

看到aegolden 的https://github.com/inkling/Subliminal/issues/23评论,新 XCode 警告的意图可能是指导您在这些枚举类型上使用不同的掩码来构造和连接各种标志。因此,不要只使用 kCGImageAlphaPremultipliedFirst,而是使用

(kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst)

(kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst)

The warning will disappear after this change.

此更改后警告将消失。