Writing an image out to a file in a cocoa app
I have a graphics editing cocoa app on Mac OSX th开发者_运维百科at 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?
If you get an NSBitmapImageRep
from the image, that can produce PNG and JPEG. See representationUsingType:properties:
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.
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];
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;
精彩评论