initWithData only works for image types that the system already knows about. For unknown types - and raw pixel data - you need to create your own image representation. You can do this through Core Graphics, as suggested in the answer referenced by Kirby. Alternatively, you can use NSImage by creating and adding NSBitmapImageRep .
The exact details will depend on the format of your pixel data, but here is an example of a process for grayscale images, where the source data ( samples array) are presented as double in the range [0,1]
NSBitmapImageRep *greyRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: nil
EDIT (in response to comment)
I did not know for sure whether 16-bit samples were supported, but you seem to have confirmed that they are.
What you see is still processing the pixels as an unsigned char , which is 8 bits. Thus, you set only half of each row, and you set each of these pixels one byte for two bytes 0xFF00 - not quite white, but very close. The other half of the image did not touch, but would be initialized to 0, so it would remain black.
You need to work 16 bits instead, first running the value returned from rep:
unsigned short * pix = (unsigned short*) [greyRep bitmapData];
And then assigning 16 bits to the pixels:
if ( j % 2 ) { pix[i * rowBytes + j] = 0xFFFF; } else { pix[i * rowBytes + j] = 0; }
Scratch that rowBytes is in bytes, so we need to stick with an unsigned char for pix and use it when assigning, which is a bit uglier:
if ( j % 2 ) { *((unsigned short*) (pix + i * rowBytes + j * 2)) = 0xFFFF; } else { *((unsigned short*) (pix + i * rowBytes + j * 2)) = 0; }
(I switched the order of the sentences because == 0 seemed redundant. Actually, for something like that, it would be a lot neater to use the ?: Syntax, but thatβs enough C futzing.)