0
我使用tableview显示视频,并希望将URL /数据保存在缓存中,因此用户在重播时不必再次下载相同的视频。在桌面视图中缓存和保存视频
但是,我想保留他们,直到用户退出应用程序。下次他打开应用程序时,他必须再次下载它们,所以我只需要将它们保存为应用程序的活动生命周期。
我还应该做缓存还是有更有效的方法来做到这一点?
-(void)loadVideo:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* file = [cachePath stringByAppendingPathComponent:@"/EGOCache.plist"];
NSDictionary *dict =[NSDictionary dictionaryWithContentsOfFile:file];
if ([dict objectForKey:urlString])
{
NSData *data = [[EGOCache globalCache] dataForKey:urlString];
data = [NSURLConnection sendSynchronousRequest:
[NSURLRequest requestWithURL:url]
returningResponse:nil
error:nil];
NSLog(@"loading from cache %@",urlString);
}else{
NSData *data = [NSURLConnection sendSynchronousRequest:
[NSURLRequest requestWithURL:url]
returningResponse:nil
error:nil];
[[EGOCache globalCache] setData:data forKey:urlString];
NSLog(@"saving cache %@",urlString);
}
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
[self.videoPlayer setContentURL:[request URL]];}
我使用的MPMoviePlayerController。还有其他建议吗? – durazno
更新了我的回答 – bsarrazin
我不明白你的意思是URL-TO-FILE-ON-DISK。更新了问题。 (顺便说一句,我的代码有问题,我认为它不会保存在缓存中) – durazno