1

I have a view with an UIImageView. It was loaded with either jpg or png image format. Says the jpg image version has a data size of 100k. I then used NSKeyedArchiver to save my view to a file. The size of the file is getting about 3MB. If I used the png version of the image, the archived file is still around 3MB.

I then used zlib to compress the archived file. But it does not seem to have much effect.

Anything I can do to get it back to close as much as possible the 100k area?

BTW: If I used UIWebView to load the image, then with the jpg version of image I will get the archived file's very close to the original jpg's.

EDIT: Matt's answer gave me an idea, so this was what I ended up doing:

Note: my I arleady used a subclass of UIImageView

  1. Create an iVar to hold NSData for the image
  2. in encodeWithCoder before calling [super encodeWithCoder...], set self.image = nil; and set self.image back to the image data using the saved NSData in 1.
  3. make sure encode the NSData iVar in 1.

In the initWithCoder, using the NSData iVar to restore the image.

user523234
  • 14,323
  • 10
  • 62
  • 102
  • The reason it is 3MB is undoubtedly because you are saving it *uncompressed*. It doesn't matter what your source file is. `UIImage` can only understand bitmap so that is what you will be saving. As matt says, why do you need to do this at all? – borrrden May 13 '13 at 04:27
  • I do need to use NSKeyedArchiver in this case. But see my EDIT. – user523234 May 13 '13 at 14:15

1 Answers1

1

You don't need a keyed archiver for this.

If the image came from inside your app, you've already got the image file, so there's nothing to save; just archive the image's name.

If the image came from, say, the Internet, all you really need to do here is save the original image in a file (and, again, possibly archive the path to the file, so you can recover it later). You had a JPG image that was 100K at some point, so why not just capture that, at the time when you have it? If it arrived as NSData, well, there's your data. If not, you can turn it into JPEG data with UIImageJPEGRepresentation. Either way, the NSData can be saved directly to a file.

The most flexible and powerful way to save (and later read) image data is with the ImageIO framework, though.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Well, in this case I do need to use keyed archiver... But your answer did remind me something... the NSData So I found a workable solution based on it. Thanks. See my Edit if you are interested. – user523234 May 13 '13 at 14:07