CGBitMapContextCreate Method Causes Compiler Warning Xcode 5 not Xcode 4
Asked Answered
R

2

24

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.

Here is the line of code eliciting the warning in 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.

Romanic answered 16/9, 2013 at 14:59 Comment(2)
You can replace kCGImageAlphaPremultipliedFirst with (CGBitmapInfo)kCGImageAlphaPremultipliedFirst.Minny
See #17246287Eisen
D
37

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.

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

(CGBitmapInfo)kCGImageAlphaPremultipliedFirst
Despite answered 12/11, 2013 at 13:21 Comment(0)
I
6

Saw a comment https://github.com/inkling/Subliminal/issues/23 by 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

(kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst)

The warning will disappear after this change.

Instrumentalism answered 26/9, 2013 at 3:31 Comment(3)
That expression only discards type information, and only happens to fix the problem by mistake (by implicitly downcasting to int then implicitly upcasting to CGBitmapInfo) (kCGmageAlphaPremultipliedFirst already only contains the bits set in kCGBitmapAlphaInfoMask). So, it's junk code that works in a roundabout manner. If you want to cast, just cast instead.Despite
@nevyn: You're right that including kCGBitmapAlphaInfoMask doesn't change the value of the argument, but the method asks for a CGBitmapInfo so I think it's nice to use a value from that enumerated type. This is, admittedly, a stylistic decision, a reminder for myself and future maintainers that what I'm really delivering is a CGBitmapInfo, and there are other bits of bitmap info I could include, even if right now I care only about the alpha info. Perhaps the cast works as well for that reminder, but I feel it is less enlightening than using the value from CGBitmapInfo explicitly.Halfwitted
Yeah alright, that's a good argument. I can't change my downvote though, it's locked :(Despite

© 2022 - 2024 — McMap. All rights reserved.