gpt4 book ai didi

ios - 缓存下载的数据

转载 作者:行者123 更新时间:2023-11-29 01:33:00 25 4
gpt4 key购买 nike

我正在使用 Collection View 在每个单元格中显示产品数据。每次用户刷新 collectionview 时,它都会再次从服务器获取数据。

我需要知道是否有可用的缓存机制来缓存下载的图像。我只想下载未缓存的最近更新的产品。

NSURL *url = [NSURL URLWithString:@"http://www.myURL/productAll.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSError *myError = nil;
id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
items = [[NSMutableArray alloc] init];
for (int i=0; i<[res count]; i++) {
NSString *arrayResult = [[res objectAtIndex:i]objectForKey:@"image"];
NSData *data = [[NSData alloc] initWithBase64EncodedString:arrayResult options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *captcha_image = [[UIImage alloc] initWithData:data];
[items addObject:captcha_image];
}
[collectionView reloadData];
}];

最佳答案

像这样使用 NSURLCache 设置内存和磁盘缓存:

int cacheSizeMemory = xxxxx; //in memory cache
int cacheSizeDisk = xxxxx; //disk cache
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];

关于ios - 缓存下载的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33265927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com