Taming Snow Leopard: CGDisplayBitsPerPixel deprecation

This one is tricky, since Apple does not provide you with an alternative API. There is one hidden in the header files however, replace:

 ..
	bitsPerPixel = CGDisplayBitsPerPixel(CGMainDisplayID());
..

with:

-- (size_t) displayBitsPerPixel:(CGDirectDisplayID) displayId {
	
	CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayId);
	size_t depth = 0;
	
	CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(mode);
	if(CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
		depth = 32;
	else if(CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
		depth = 16;
	else if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
		depth = 8;
	
	return depth;
}

...
	bitsPerPixel = [self displayBitsPerPixel: CGMainDisplayID() ];
...

Advertisements
Tagged with: ,
Posted in Apple, Code
4 comments on “Taming Snow Leopard: CGDisplayBitsPerPixel deprecation
  1. macuserone says:

    Thanks for the code! very helpful.
    how about CGDisplayBytesPerpixel, do you have any suggestions?

  2. nobody says:

    Thanks for the code. It worked.

  3. Andrew says:

    Thanks dude, this helped alot! Had no idea how to use the CFStringRef I got back from copyPixelEncoding!! Once again, thanks a bunch!

  4. Stephen Checkoway says:

    You’re leaking memory. You need to call:

    CGDisplayModeRelease(mode);
    CFRelease(pixEnc);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories
%d bloggers like this: