views:

43

answers:

2

Can someone point out a comprehensive example on Image processing using cocoa API? I am developing an application for the Mac, and not for the iphone device. I usually come across with UIImage manipulation which provides an intuitive set of methods to achieve task such as per pixel manipulation and saving into file at different format. In the case with Appkit, NSImage I really find it hard to manipulate per pixel data of the images and saving to different file formats such as PNG not just TIFF.

A: 

You can retrieve a bitmap representation of your image object and modify its data

NSBitmapImageRep *rep = [[image representations] objectAtIndex: 0];
unsigned char *bmpData = [rep bitmapData];

To save modified representation in PNG format do the following:

NSData *data = [bits representationUsingType: NSPNGFileType properties: nil];
[data writeToFile: @"/path-to-your-file/image.png" atomically: NO];
Pixie
Note that there may be multiple representations, and #0 is not guaranteed to be the one you want. Also, manipulating the pixels you get from `bitmapData` will not necessarily feed through to change the image contents. If you want to make changes you should probably then create a new `NSBitmapImageRep` and `NSImage` from the modified data.
walkytalky
+2  A: 

If you want to work with pixels, CGImage and CGImageSource and CGImageDestination are the way to go. Unlike AppKit's NSImage, which is designed generally in order to handle any kind of image, the CGImage classes are specifically designed for raster images.

Peter Hosey