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.

jac300
  • 5,182
  • 14
  • 54
  • 89
  • 14
    You can replace `kCGImageAlphaPremultipliedFirst` with `(CGBitmapInfo)kCGImageAlphaPremultipliedFirst`. – Rob Sep 16 '13 at 15:11
  • 2
    See http://stackoverflow.com/questions/17245787/how-do-i-create-an-alpha-only-bitmap-context/17246355#17246355 – rmaddy Sep 16 '13 at 15:12

2 Answers2

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
nevyn
  • 7,052
  • 3
  • 32
  • 43
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.

CodeBrew
  • 6,457
  • 2
  • 43
  • 48
  • 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. – nevyn Nov 12 '13 at 13:19
  • 3
    @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. – Aaron Golden Jan 16 '14 at 02:38
  • Yeah alright, that's a good argument. I can't change my downvote though, it's locked :( – nevyn Jan 16 '14 at 10:00