简体   繁体   English

iPhone存储图像的最佳方法

[英]iphone best way to store images

I'm developing an app that needs to cache some images from the web the images are likely to be 100x100 我正在开发一个需要从网络缓存一些图像的应用程序,这些图像可能是100x100

I just need to know which is better: 我只需要知道哪个更好:

  1. Store the images as files in the iPhone file system 将图像作为文件存储在iPhone文件系统中
  2. Store them as blob in a sqlite Db along with other data already saved on the database. 将它们作为blob与已经保存在数据库中的其他数据一起存储在sqlite Db中。

Appreciate your help. 感谢您的帮助。

Storing images in the database is definitely not recommended by Apple, the filesystem is actually really good at locating files (images in this case) on the disk, which makes it the best solution for saving images. Apple绝对不建议在数据库中存储图像,文件系统实际上非常擅长在磁盘上定位文件(在这种情况下为图像),这使其成为保存图像的最佳解决方案。 These were the exact same words an Apple technology evangelist used in the iPhone Tech Talk World Tour in Paris. 这些是与苹果在巴黎的iPhone Tech Talk世界巡回演唱会中使用的苹果技术推广人员所说的完全相同的词。 If it are only a few images, you might get away with it, but when the number can potentially become quite large, files is the way to go. 如果只是几张图像,您可能会避开它,但是当数量可能会变得很大时,就可以使用文件。

Besides, you can use the lazy loading methods to grab the images of the disk, this will delay loading the image from disk to when it really is needed. 此外,您可以使用延迟加载方法来获取磁盘映像,这会将从磁盘加载映像的时间延迟到真正需要时。

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image.png"]]

Edit: imageWithContentsOfFile does not load the image at once, thus not locking the thread, neither does it cache the image. 编辑:imageWithContentsOfFile不会立即加载图像,因此不会锁定线程,也不会缓存图像。 The other method imageNamed: does lock the thread and loads the image at once, on the bright side, it does cache the image. 另一个方法imageNamed:确实锁定线程并立即加载图像,但在好的一面,它确实缓存了图像。

From past experience, storing (and retrieving) images from the file system should be a bit faster than from the DB. 根据过去的经验,从文件系统中存储(和检索)图像应该比从数据库中更快。 As for ease of handling and maintenance it only depends on what you're more familiar with: SQL scripts or iphoneOS file system functions. 为了简化操作和维护,它仅取决于您更熟悉的内容:SQL脚本或iphoneOS文件系统功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM