I am developing an application to draw and process specific image format not currently supported by the NSimage class. In this specific format, the images are in form of a 1D binary table preceded by an ASCII header which describe the data (number dimension, image size, bit per pixel, ...).
I read in the Coco Drawing Guide that it is possible to create new image representation. I am new in Coca and Objectiv-C programming and, on this topic, I find the Coco Drawing Guide explanation very short and not so helpfull to me. I am now interested in getting more detail with some example code on how to create new image representation. Also I am particularly interested in understanding how to overwrite the initWithData and Draw method in order Cocoa application correctly interpret the new image representation.
Does any know share with me a link where the method is described in more detail ?
                        
If all you want to do is draw your image format and don't particularly care about passing it back out of your app again, you'd be best off forgetting about subclassing
NSImageRepand just creating anNSBitmapImageRepwith a bit depth and dimensions matching your image. You then ask it for its-bitmapData.That will give you a raw buffer which you can directly address, and you'd then literally copy each of the pixels of your image one by one into the appropriate locations in the buffer.
If you really do want a custom
NSImageRep, then all you need to do is subclassNSImageRepand then in yourinitWithData:method you would store the image data in an ivar and then call all the various methods to tell the image rep how it should be configured, such as-setBitsPerSample:,-setPixelsHigh:etc. The documentation for theNSImageRepclass explains which methods should be called when initialising the image.To implement
-drawyou literally need to draw your pixels in the current context. Obviously how you do this depends on the type of image you're drawing (e.g. a vector format will have very different drawing code to a bitmap one).One way to draw a bitmap image would be to loop through the pixels and call
NSRectFill()with a one pixel-sized rectangle for each pixel, which is pretty fast although it's not particularly efficient.However, you'd be better off creating an
NSBitmapImageRepand manipulating the pixels in the bitmap context (returned from-bitmapData) directly. You can then just draw theCGImageRepthat you can get from theNSBitmapImageRep. If you create theNSBitmapImageRepin yourinitWithData:method, you would only have to assign the pixels once.