문제

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)?

도움이 되었습니까?

해결책

Use:

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top