Search code examples
objective-cmacoscocoansimage

Is it possible to check if an image stored in an NSimage object is grayscale?


Is there a simple method for checking if an NSImage Object contains a grayscale image?

What I am trying to do is count the number of colour and black and white pages within a pdf. As far as I can tell the only real solution to this is to render the pdf pages as images and then work from there. I didn't want to have to check every pixel if possible but so far that's all I can come up with.


Solution

  • You can access the NSImage's -representations (which returns an array of NSImageRep instances) and query the returned NSImageReps for these details.

    Primary NSImageRep and CGImage details of interest include:

    • assigned colorspace
    • the number of components from the image's bitmap info, bits per component, and bits per pixel.