简体   繁体   English

多个图像存储和表视图处理

[英]Multiple Image storing and dealing with table view

Despite there are a lot of themes around this topic, I seem very frustrated in the following situation :( 尽管围绕该主题有很多主题,但在以下情况下,我似乎非常沮丧:(

The app downloads and parses a JSON with about 1000 objects, which have in their attributes link for a small image. 该应用程序下载并解析一个包含约1000个对象的JSON,这些对象的属性链接中包含一幅小图像。 I need to download (once) these images, store them and use in my TableView as icons. 我需要下载(一次)这些图像,将它们存储并在TableView中用作图标。

What is the best way to store such amount of files and to work efficiently with them while displaying the TableView? 在显示TableView的同时,存储如此大量的文件并有效地使用它们的最佳方法是什么?

I will tell you how will I do it but before I would like to give you a warning about downloading files from the Internet and storing them in your app. 我将告诉您我将如何做,但在此之前,我想警告您有关从Internet下载文件并将其存储在您的应用程序中的警告

I got one of my apps rejected from Apple lately because I violated rule 2.23 from the App Store Review Guidelines which says: 我最近拒绝了我的一个应用程序,因为我违反了《 App Store审查指南》中的规则2.23,其中说:

2.23 Apps must follow the iOS Data Storage Guidelines or they will be rejected 2.23应用必须遵循iOS数据存储准则 ,否则将被拒绝

Since Apple is using iCloud lately you should minimize the traffic that each app will upload and therefore you should not have more than 40MB stored in your Documents folder of your app. 由于Apple最近使用的是iCloud,因此应尽量减少每个应用程序上载的流量,因此,应用程序的Documents文件夹中存储的内存不得超过40MB。 They also say (if you read the links I provided): 他们还说(如果您阅读了我提供的链接):

Data that can be downloaded again or regenerated should be stored in the /Library/Caches directory. 可以再次下载或重新生成的数据应存储在/ Library / Caches目录中。

And since the cache folder can be deleted by the iOS if needed, you should design your app to be prepared to download the images again in case they are gone, so keep this in mind. 而且,由于可以根据需要通过iOS删除缓存文件夹,因此您应将应用程序设计为准备好在图像消失后再次下载图像,因此请记住这一点。

Now for my solution, I think downloading 1000 files is too much (even if they are small files). 现在,对于我的解决方案,我认为下载1000个文件太多了(即使它们是小文件)。 I suggest that you have the files saved in a ZIP file on the server from which you are willing to download and unzip them on your disc once downloaded. 我建议您将文件保存在服务器上的ZIP文件中,从中下载文件,并在下载后将其解压缩到光盘上。 This will be much easier and more practical. 这将更加容易和实用。 You can look here to see how to zip and unzip in your app. 您可以在此处查看如何在您的应用程序中进行压缩和解压缩。

After you have your files unzipped I suggest to have a small database (SQLite) from which you can load load the file names, store these names in an array, and then use this array to fill the images in your table in the function cellForRowAtIndexPath . 将文件解压缩后,建议您使用一个小型数据库(SQLite),从中可以加载文件名,将这些名称存储在数组中,然后使用此数组将图像填充到函数cellForRowAtIndexPath中的表中。

I hope this helps you. 我希望这可以帮助你。 By the way, it is my way to do it but I am not saying it is the "best way" as you are asking :) 顺便说一句,这是我的方式,但我并不是说这是您要求的“最佳方式” :)

Given that you would like to keep these images/data persistent, you now have two options: You can manage them yourself or you can utilize Core Data to help you do it. 鉴于您想保持这些图像/数据的持久性,现在有两个选择:您可以自己管理它们,也可以利用Core Data来帮助您做到这一点。 I recommend Core Data. 我推荐核心数据。

If your not familiar with Core Data, you can start reading about it Here . 如果您不熟悉Core Data,可以在此处开始阅读。 It's a little bit daunting to get started, but once you've run through a tutorial it's extremely straightforward and will make your life much easier. 入门有点艰巨,但是一旦您完成了教程,它就会非常简单明了,并使您的生活变得更加轻松。

If you would like store the images yourself, or would like a more efficient Core Data structure, I recommend writing all of your images into your Documents directory as see Here . 如果您想自己存储图像,或者想要更有效的Core Data结构,我建议将所有图像写入Documents目录,如here所示 Then you can just store File path strings in your CD structure or manually in your plist like This . 然后,您可以将文件路径字符串存储在CD结构中,或者手动存储在plist中,例如This

And finally, for loading them into your TableView you can use those stored image paths to load UIImages with: 最后,要将它们加载到TableView中,可以使用以下存储的图像路径加载UIImage:

[[UIImage alloc] initWithContentsOfFile:filePath];

This would be placed within your: 这将放在您的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

function so that you can set the image where you would like. 功能,以便您可以将图像设置在所需位置。

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

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