在iPhone应用程序中,在web上存储图像和数据最有效的方法是什么?

时间:2022-09-23 17:48:12

I'll try to keep this as short as possible.. I am making an app for iPhone that needs to be able to store some images from the web. The URL for the images(among other data) are stored in one table at a MySQL-server. From there, I have parsed the table with JSON, and successfully serialized this to show in a UITableView in the app. Each cell now shows the image from the imageurl-field in the table, and the title of the image as text, from the title-field in the table, both from MySQL->json.php->NSArray(json)(objectAtIndex) etc.

我尽量把它写得简短些。我正在为iPhone开发一个应用程序,它需要能够从web上存储一些图像。图像的URL(以及其他数据)存储在sql -server的一个表中。从那里,我有解析JSON的表,并成功地序列化这显示UITableView应用。每个单元现在显示的图像imageurl-field表,和形象的标题文本、标题字段的表,从MySQL - > json.php - > NSArray(JSON)(,objectAtIndex)等。

At this point, every time the App launches, it gets all the information from the url with JSON.php every time, and I think it downloads the image every time aswell. I feel that this is way more work than necessary, but, as the database is going to update itself irregulary based on where it gets its data, it's not good enough to just download the information once.

此时,每次应用程序启动时,它都使用JSON从url获取所有信息。每次都是php,我想它也会下载图像。我觉得这是比必要的更多的工作,但是,由于数据库将根据它获取数据的位置来更新它自己,所以仅仅下载一次信息是不够的。

Question 1: I need a solution where the app "asks" the .php if it has the same exact data as the JSON shows. The most logical way I can think of is to add a field for "date" in the .php, showing which date the data was last updated, and if the app has data from the same date, nothing happens, but if the php-side date is newer than the app-side date, then it must be able to delete all stored data, and replace with the new JSON-data.

问题1:我需要一个解决方案,如果.php的数据与JSON显示的数据相同,应用程序就会“问”这个问题。我能想到的最符合逻辑的方法是添加一个字段为“日期”。php,显示数据最后更新日期,如果应用程序数据来自同一个日期,什么也不会发生,但如果php日期比app-side更新日期,那么它必须能够删除所有存储数据,并更换新的json数据。

Question 2: The JSON-data is an NSArray in the iPhone, and the field for "Image" is just the url to the image (www.example.com/image.png), and not the actual image. How can I download these images and save them on the iPhone until a new "version" of the data is available through JSON?

问题2:JSON-data是iPhone中的NSArray,“Image”字段只是图像的url (www.example.com/image.png),而不是实际的图像。如何下载这些图像并保存在iPhone上,直到JSON提供了新的数据“版本”?

Bonus question: The method I am using now is making the UITableView lagging when I scroll. I am no pro, but I have read several places that this can be caused by leaking memory, and I have not completely understood what this means. I also read that it can be because I maybe instantiate objects for every cell, instead of once and re-use it.

附加问题:我现在使用的方法是在滚动时使UITableView变得滞后。我不是专业人士,但我在很多地方读到过,这可能是由于内存泄漏造成的,我还没有完全理解这意味着什么。我也读到它可能是因为我可能为每个单元实例化对象,而不是一次,然后重新使用它。

Bottom line: My goal is to just have the app download all the data from the json.php once at the first app-launch, and at later launches just check if the versions are correct. If correct, no download necessary what so ever, just use the stored JSON-object containing data, and the image-files from the urls downloaded the first launch. If not correct, delete all previous stored images AND clear the json-object, and import the new updated json-object, and saving the images from the image-urls from the jsonobject somewhere on the iPhone.

底线:我的目标是让应用程序从json中下载所有数据。php在第一次应用程序启动时,然后在稍后的启动中检查版本是否正确。如果正确的话,不需要下载什么,只要使用存储的json对象包含的数据,以及从url下载的图像文件。如果不正确,则删除所有先前存储的图像并清除json对象,并导入新的更新的json对象,并从iPhone上某个地方的jsonobject的图像url中保存图像。

Note: Updating the data (i.e adding one extra image it that is all the change that has been made) is not a solution, and not a possibility in my eyes here. When there has been an update in this database, I need ALL the information stored in the phones to clear out, and redownload all. This is because the information I put in the database, is parsed from another website, and if the new images or updates to the data contains changes of some other field, then I will much rather tweak the database to fit(through json.php) than releasing update for the app all the time.

注意:更新数据(i)。添加一个额外的图像,这是所有已经做的改变)不是一个解决方案,在我的眼睛这里是不可能的。当这个数据库有更新时,我需要手机中存储的所有信息清除并重新下载。这是因为我把数据库中的信息,解析从另一个网站,如果新图像或更新数据包含一些其他领域的变化,然后我会更愿意调整数据库适合(通过json.php)比发布更新的应用程序。

Is this the wrong approach? Is this possible? Help is much appreciated!

这是错误的做法吗?这是可能的吗?感谢帮助!

EDIT JSON-stuff from here and there..

从这里到那里编辑json内容。

#define kURL [NSURL URLWithString:@"www.example.com"] 

NSData* data = [NSData dataWithContentsOfURL: kURL]; 
NSArray *json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e]; 
NSDictionary *info = [json objectAtIndex:indexPath.row]; 
cell.textLabel.text = [info objectForKey:@"Name"]; // ±Same for url

This is extracted from different places in the class, so they might not work exactly like this..

这是从类中的不同地方提取的,所以它们可能不会像这样工作。

EDIT

编辑

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}



// Configure the cell...

info = [json objectAtIndex:indexPath.row];

cell.textLabel.text = [info objectForKey:@"Name"];


NSString* imageURL = [info objectForKey:@"Image"];
NSURL *URLimage = [NSURL URLWithString:imageURL];
NSData* imageData = [NSData dataWithContentsOfURL:URLimage];
UIImage* images = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = images;




return cell;

}

}

Stian.

Stian。

2 个解决方案

#1


0  

I assume you're communicating with your server through NSURLRequest. If so, just set the cache policy to NSURLRequestReloadRevalidatingCacheData and it won't re-download your data if it's already in the cache and up to date. This way it pretty much gets handled for you.

我假设您是通过NSURLRequest与您的服务器通信的。如果是这样,只需将缓存策略设置为NSURLRequestReloadRevalidatingCacheData,如果数据已经在缓存中并且是最新的,那么它不会重新下载数据。这样你就能搞定了。

You'll also want to set up an NSURLCache in your app delegate somewhere.

您还需要在应用委托中设置一个NSURLCache。

I can't tell you what's causing your chunky scrolling if you don't post your code.

如果你不上传你的代码,我无法告诉你是什么原因导致你的滚动。

#2


0  

Just incase someone reads this.. When making a NSURL to fetch (and parse) a large JSON file for example. You will want to do this in the background (dispatch_get_global_queue). Then call it when you want the data (dispatch_async). Otherwise you are using the same Thread as your UI and it will freeze while performing the task..

以防有人读到这个。例如,在创建NSURL以获取(和解析)大型JSON文件时。您将希望在后台执行此操作(dispatch_get_global_queue)。然后在需要数据时调用它(dispatch_async)。否则,您将使用与UI相同的线程,它将在执行任务时被冻结。

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define JSONURL [NSURL URLWithString:@"http://www.web.com/json.php"]    
...

dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL:JSONURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

#1


0  

I assume you're communicating with your server through NSURLRequest. If so, just set the cache policy to NSURLRequestReloadRevalidatingCacheData and it won't re-download your data if it's already in the cache and up to date. This way it pretty much gets handled for you.

我假设您是通过NSURLRequest与您的服务器通信的。如果是这样,只需将缓存策略设置为NSURLRequestReloadRevalidatingCacheData,如果数据已经在缓存中并且是最新的,那么它不会重新下载数据。这样你就能搞定了。

You'll also want to set up an NSURLCache in your app delegate somewhere.

您还需要在应用委托中设置一个NSURLCache。

I can't tell you what's causing your chunky scrolling if you don't post your code.

如果你不上传你的代码,我无法告诉你是什么原因导致你的滚动。

#2


0  

Just incase someone reads this.. When making a NSURL to fetch (and parse) a large JSON file for example. You will want to do this in the background (dispatch_get_global_queue). Then call it when you want the data (dispatch_async). Otherwise you are using the same Thread as your UI and it will freeze while performing the task..

以防有人读到这个。例如,在创建NSURL以获取(和解析)大型JSON文件时。您将希望在后台执行此操作(dispatch_get_global_queue)。然后在需要数据时调用它(dispatch_async)。否则,您将使用与UI相同的线程,它将在执行任务时被冻结。

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define JSONURL [NSURL URLWithString:@"http://www.web.com/json.php"]    
...

dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL:JSONURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});