Re: File sizes when NSImage's are included
Re: File sizes when NSImage's are included
- Subject: Re: File sizes when NSImage's are included
- From: Nicko van Someren <email@hidden>
- Date: Mon, 29 Dec 2003 15:23:34 +0000
On 29 Dec 2003, at 14:04, Matthew Roberts wrote:
...
When the file is saved, all the objects respond to encodeWithCoder:,
which
then calls the same for the various standard objects (including the
NSImage).
All very standard--and it works great. However, either I've done
something
wrong or this is an incredibly inefficient way to store images. I
drag a
photo to the ImageView--just a 20k JPEG, say 400x300. When the file is
saved, the document file size has jumped by over 500K!
Well 500K is about what you'd expect for an uncompressed raw pixel map
of that size in 32bpp. It sounds like encodeWithCoder: does not even
try to compress the data.
I've even now made it so that the image dragged into the ImageView is
not
the image saved; instead, I use imageRep's to make sure it's stored as
JPEG
How are you doing this? I think you'll find that the NSBitmapImageRep
always keeps stuff internally as a bitmap and only offers options to
write the data out in different formats using
-representationUsingType:properties:
(I would have thought it was already),
Apparently not. I sent a mail to this list a few days ago trying to
find out how to retrieve the original data from which the NSImage came
but I've not seen any replies to the post.
add a goodly amount of compression,
and then make all images no taller than 200 pixels. And yet, I'm still
getting filesize increases of 500k for a simple 20k picture.
That's because the serialisation of the NSImageRep still contains
enough data to rebuild the object without data loss.
What am I doing wrong here? I'm content with the way the program
works, but
I'd like to add every one's photos, and I've got over 90 students. I
don't
want a simple data file that's 50Mb.
What I think you are going to need to do is extract a NSBitmapImageRep
from the NSImage and then use -representationUsingType:properties: to
get a compressed image out (as NSData) that you can save. Ideally you
want to retrieve the original data that was used to make the image but
I have yet to work out how to do that. (If anyone knows PLEASE let us
know).
Note that if you use this method you'll want to be careful only to
change the NSData of the compressed image if the user has actually
altered the image. There are two reasons for this; one is that
re-compressing the data every time would be slow but the other, more
important issue is that JPEG is lossy and if you keep unpacking the
data and re-compressing then you will rapidly loose image quality. You
could avoid this by using LZW compressed TIFF images instead of JPEG,
although the images will still be bigger than the original.
Nicko
_______________________________________________
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.