压缩包含UIImageView和已加载图像的NSKeyedArchiver文件 [英] Compressing an NSKeyedArchiver file that included UIImageView with loaded image

查看:82
本文介绍了压缩包含UIImageView和已加载图像的NSKeyedArchiver文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有UIImageView的视图.它以jpg或png图像格式加载.说jpg图片版本的数据大小为100k.然后,我使用NSKeyedArchiver将视图保存到文件中.文件大小约为3MB.如果我使用图片的png版本,则存档文件仍约为3MB.

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.

然后,我使用zlib压缩了存档文件.但这似乎没有太大作用.

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

我能做些什么使它尽可能接近100k区域吗?

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

顺便说一句:如果我使用UIWebView加载图像,那么使用图像的jpg版本,我将获得与原始jpg非常接近的存档文件.

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.

Matt的回答给了我一个主意,所以这就是我最终要做的事情:

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

注意:我的助手使用了UIImageView的子类

Note: my I arleady used a subclass of UIImageView

  1. 创建一个iVar以容纳图像的NSData
  2. 调用[super encodeWithCoder...]之前在encodeWithCoder中的
  3. 中设置self.image = nil;并使用1中保存的NSDataself.image设置回图像数据.
  4. 确保将NSData iVar编码为1.
  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.

initWithCoder中,使用NSData iVar还原图像.

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

推荐答案

您不需要为此使用键控存档器.

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.

如果图像是从Internet发出的,那么您真正需要做的就是将原始图像保存在文件中(同样,可以将文件的路径存档,以便以后可以恢复它).您有时有一个100K的JPG图像,那么为什么不立即在拥有它时就捕获它呢?如果它是作为NSData到达的,那么,就有您的数据了.如果没有,您可以使用UIImageJPEGRepresentation将其转换为JPEG数据.无论哪种方式,NSData都可以直接保存到文件中.

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.

种灵活而强大的方法来保存(和以后读取)图像数据,是通过ImageIO框架实现的.

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

这篇关于压缩包含UIImageView和已加载图像的NSKeyedArchiver文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆