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?
+1
A:
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.
Marc W
2009-10-27 19:26:38
+1
A:
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];
Boiler Bill
2009-10-27 19:31:55
+3
A:
If you get an NSBitmapImageRep
from the image, that can produce PNG and JPEG. See representationUsingType:properties:
Jim Zajkowski
2009-10-27 19:34:52
But how do you get an NSBitImageRep from an NSImage?
Mike2012
2009-11-11 00:59:38
A:
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;
SEQOY Development Team
2009-10-28 20:51:57