Pergunta

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.

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top