Re: [newbie] Saving NSImage's
Re: [newbie] Saving NSImage's
- Subject: Re: [newbie] Saving NSImage's
- From: Jan Van Tol <email@hidden>
- Date: Thu, 10 Oct 2002 13:13:29 -0500
This is almost exactly the problem I have. The documentation for
TIFFRepresentation and company confused me too, so that's why I'm
looking for an example.
-Jan Van Tol
On Thursday, October 10, 2002, at 09:47 AM, John Pannell wrote:
Hi there-
I was hoping someone would write a better solution than mine, but I
haven't seen much yet... here's what I did. My application pulls an
image from a URL, like so:
NSData *data = [myURL resourceDataUsingCache:NO];
... and then creates an NSImage by:
NSImage *theImage = [[NSImage alloc] initWithData: data];
This worked great until I ran into the problem you are having: I want
to
write the image to disk, which is very convenient with NSData, but less
so with NSImage (I'm sure it must be possible, it's just that I'm no
expert and the docs confused me on this issue). My solution was to
write the data to disk in the /tmp directory right when I got it, like
so:
NSData *data = [myURL resourceDataUsingCache:NO];
filePath = [@"/tmp/PSM_NS_" stringByAppendingString:imageNumber];
filePath = [filePath stringByAppendingString:[fileNames
objectAtIndex:[imageNumber intValue]]];
[data writeToFile:filePath atomically:YES];
NSImage *theImage = [[NSImage alloc] initWithData: data];
The two middle lines about filePath are to establish a unique name and
preserve the ending (.jpg, .gif, .png, whatever) of the file.
Later, when the user has selected an image and wants to save it, I fish
it out of the /tmp directory and save it wherever the user wanted it
(the following is within the callback method for a save sheet):
sourcePath = [@"/tmp/PSM_NS_" stringByAppendingString:[[NSString
alloc] initWithFormat:
@"%d",[[theImageMatrix selectedCell] tag]]];
sourcePath = [sourcePath stringByAppendingString:[fileNames
objectAtIndex:
[[theImageMatrix selectedCell] tag]]];
imageData = [[NSData alloc] initWithContentsOfFile:sourcePath];
[imageData writeToFile:[sheet filename] atomically:YES];
The sourcePath lines deal with the unique naming convention and how I
stored the names of the files internally. Then I just init a new
NSData
with the file I stored in /tmp, and write the data where the user
requested.
There must be a "real" way to do this, but this does have the
advantages
of working with any graphic file type, and being simple to understand
(something very important to me :-)
Hope this helps!
John P.
On Wednesday, October 9, 2002, at 04:46 PM, Jan Van Tol wrote:
I'm looking for a simple example of how to save an NSImage to a file.
Right now I download an image file, and then I can save the NSData
object I get just fine. But now I want to be able to save my NSImage.
Do I need to convert my NSImage to an NSData object? If so, how?
Thanks in advance,
-Jan Van Tol
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.