views:

65

answers:

1
NSImage * strImage = [[NSImage alloc]initWithContentsOfFile:ImageFilePath] ;
NSData *imageData = [strImage TIFFRepresentation];

now what I required is to convert imagedata into JPEG format. I want to work same as in QImage save() do.

for the help how save() works i give you link below

http://doc.qt.nokia.com/4.6/qimage.html#save

plz help me..

+1  A: 

From CocoaDev... If you do anything with the NSImage after creating it:

NSData *imageData = [image TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSNumber *compressionFactor = [NSNumber numberWithFloat:0.9];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:compressionFactor
                                         forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:filename atomically:YES];

If you don't really do anything else with the NSImage before:

NSArray *representations = [myImage representations];
NSNumber *compressionFactor = [NSNumber numberWithFloat:0.9];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:compressionFactor
                                         forKey:NSImageCompressionFactor];
NSData *bitmapData = [NSBitmapImageRep representationOfImageRepsInArray:representations 
                                       usingType:NSJPEGFileType 
                                       properties:imageProps];    
[bitmapData writeToFile:filename atomically:YES];
Georg Fritzsche
thanks alot,it works.
Greshi Gupta
But what a waste of time; you're getting the TIFF representation of an image, but then immediately creating an image rep from that! If your source image already has a bitmap representation, use that.
Mike Abdullah
@Mike: That won't work in all cases, see the CocoaDev link.
Georg Fritzsche
What can I do if I want reverse of this,that means from JPEG format data into NSImage Data???
Greshi Gupta
@Greshi: Asking a new question? ;)
Georg Fritzsche