Example code programming Objective-C with Cocoa in Xcode and Interface Builder (Leopard)

40: Read TIFF file into NSBitmapImageRep, edit and write it

Problem: We want to read in a bitmap image, edit it and display it in a view, then write it to disk.

Solution: Use Interface Builder to define a NSWindow and a custom NSView. Create an NSImage and initialise it with a TIFF (.tif) file.

Create an NSData object initialised to contain the TIFF bitmap data. Use that to create a NSBitmapImageRep object.

Use setPixel to modifiy the NSBitmapImageRep object then display it on screen using the drawInRect method. Convert the bitmap back into a NSData TIFFRepresentation object and use the writeToFile method to save to file.

The original .tif file of Our Garden of Eden

The original .tif file of Our Garden of Eden

Implementation.

Create an uncompressed .tif file.

Create a new project in XCode: File->New Project->Cocoa Application
Call it: 040_NSBitmapImageRep_read_write_file

Now create the class: MyNSView of type NSView.
Code it as shown below and save.

// MyNSView.h // Created by julius on 10/11/2008. #import <Cocoa/Cocoa.h> @interface MyNSView : NSView { } @end // MyNSView.m #import "MyNSView.h" @implementation MyNSView - (id)initWithFrame:(NSRect)pFrame { if ( !(self = [super initWithFrame:pFrame]) ) { return self; } // end if return self; } - (void)drawRect:(NSRect)pRect { // replace the path with your own NSString * zstringFileNamePath = @"/Users/fred/TIFF/garden_of_eden.tif"; NSImage * zNSImage = [[NSImage alloc] initWithContentsOfFile:zstringFileNamePath]; [zNSImage drawInRect: pRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; } // end drawRect @end

Click on the MainMenu.xib to go into Interface Builder. Drag a custom view onto the panel and make it the same size as the .tif.

Save and Run. You should see the .tif displayed in your NSView.

Modify the drawRect as shown below.

- (void)drawRect:(NSRect)pRect { NSString * zstringFileNamePath = @"/Users/fred/TIFF/garden_of_eden.tif"; NSImage * zNSImage = [[NSImage alloc] initWithContentsOfFile:zstringFileNamePath]; NSData *zNsDataTifData = [[NSData alloc] initWithData:[zNSImage TIFFRepresentation]]; NSBitmapImageRep *zNsBitmapImageRepObj = [[NSBitmapImageRep alloc] initWithData:zNsDataTifData]; NSUInteger zRed = round(255.0 * 0.0); NSUInteger zGreen = round(255.0 * 0.0); NSUInteger zBlue = round(255.0 * 1.0); NSUInteger zColourAry[3] = {zRed,zGreen,zBlue}; NSInteger y; for (y = 20; y < 100; y++) { NSInteger x; for (x = 20; x < 100; x++) { [zNsBitmapImageRepObj setPixel:zColourAry atX:x y:y]; } // end for x } // end for y [NSGraphicsContext saveGraphicsState]; [zNsBitmapImageRepObj drawInRect:pRect]; [NSGraphicsContext restoreGraphicsState]; } // end drawRect

Save and Run. You should see the .tif with a blue square drawn within it as shown.

The modified bitmap image of Our Garden of Eden

The modified bitmap image of Our Garden of Eden

To write the modified bitmap to file as a .tif add the following lines of code to the end of the drawRect method..

NSData *zNsDataTiffData2; zNsDataTiffData2 = [zNsBitmapImageRepObj TIFFRepresentation]; [zNsDataTiffData2 writeToFile:@"/Users/fred/TIFF/garden_of_edenBlueRect.tif" atomically:YES];

If you want to download the code

Click the Download Link to obtain 040_NSBitmapImageRep_read_write_file.zip file of this whole OS X 10.5 Leopard program.

Download 040_NSBitmapImageRep_read_write_file.zip (2.2 MB)



Please send me your comments

If you include your e-mail I may reply!  

Page last modified: 18:58 Sunday 12th. May 2013

Julius Guzy

Paintings & Drawings

  • Link to picture about the israeli and palestinian conflict entitled: how we pacified our genocidal neighbours

animatedPaint