Mike2012
Mike2012

Reputation: 7725

Writing an image out to a file in a cocoa app

I have a graphics editing cocoa app on Mac OSX that produces 32 by 32 square bitmaps, I need to programatically (I cannot use the interface builder at all) output this image to either a .jpg or .png. Can anyone link me to some good resources on how I might accomplish this task?

Upvotes: 4

Views: 2621

Answers (4)

SEQOY Development Team
SEQOY Development Team

Reputation: 1615

Using the PhotoshopFramework for iPhone (https://sourceforge.net/projects/photoshopframew/). Have very convenient methods to save images to disk:

/// Save as JPG File with High Quality.
-(BOOL)saveAsJPGFile:(NSString*)anFileName;

/// Save as JPG File with User Defined Quality.
-(BOOL)saveAsJPGFile:(NSString*)anFileName compressionQuality:(CGFloat)quality;

/// Save as PNG File.
-(BOOL)saveAsPNGFile:(NSString*)anFileName;

Upvotes: 0

Jim Zajkowski
Jim Zajkowski

Reputation: 987

If you get an NSBitmapImageRep from the image, that can produce PNG and JPEG. See representationUsingType:properties:

Upvotes: 3

Boiler Bill
Boiler Bill

Reputation: 1940

Having an UIImage called image:

NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *tmpPathToFile = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/tempImage.jpg", tempPath]];
[imageData writeToFile:tmpPathToFile atomically:YES];

Upvotes: 2

Marc W
Marc W

Reputation: 19241

I know you said jpeg or png, but you could use NSImage's TIFFRepresentation method. It returns an instance of NSData. So doing something along the lines of

[[yourImageInstance TIFFRepresentation] writeToFile:@"/path/to/file.tiff" atomically:NO];

would write that TIFF image to file. I do not think NSImage has any built-in way of getting the data in png or jpeg form.

Edit

Did a quick Google and found this link with info on saving PNG data instead of TIFF data. Seems pretty straightforward.

Upvotes: 2

Related Questions