Search code examples
objective-ccocoansdatansimage

How to tell if NSData is actually an NSImage?


I'm sending data back and forth through the Bonjour protocol. All the packets are sent as NSData and usually converted to strings; but what if I need to receive an image? This is the process that will be going on.

  1. Client requests the NSImage
  2. Server sends the requested NSImage as a NSData packet
  3. Client receives NSData
  4. Client checks if the data is an image
  5. ????
  6. PROFIT?

Could I attempt to convert the data into an image and catch the error (which would mean it's not an image)?


Solution

  • Use:

    NSImage *image = [[NSImage alloc] initWithData:data];
    

    If data is not valid image data, then image will be nil.