Determining the Colorspace of a UIImage
Asked Answered
A

2

6

I'm wondering if there's an easy way to get the colorspace of an image (i.e. an image loaded into a UIImage)? For example, I have a TIFF image and I'd like to be able to determine if it uses the RGB colorspace or not. Is there an easy way to do this without manipulating pixel data? I know there's some CGColorSpace functions, but none of them seem to do this, just create colorspaces and manipulate them (and much more advanced functions).

Thanks in advance.

Audiometer answered 12/11, 2010 at 20:55 Comment(0)
A
16

You have to get the color space through CGImage. You can do it with the following line of functions/properties:

@property(nonatomic, readonly) CGImageRef CGImage

CGColorSpaceRef CGImageGetColorSpace (
   CGImageRef image
);

So to get the color space of an image, you'd do:

CGColorSpaceRef colorspace = CGImageGetColorSpace([myUIImage CGImage]);

And of course, make sure to follow the get/create/copy rules for CG objects.

Alphabetic answered 12/11, 2010 at 20:59 Comment(0)
O
0

Swift

To obtain the color space of a UIImage:

let colorSpace = myUIImage.cgImage?.colorSpace

Example code that checks the color space type:

if colorSpace == CGColorSpace(name: CGColorSpace.sRGB) && colorSpace != nil {
  print("valid sRGB")
}

See Apple's CGColorSpace Documentation "Accessing System-Defined Color Spaces" for the complete list of color space types.

Objectify answered 6/7, 2021 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.