在tableview中缓存和保存视频

时间:2015-09-09 13:28:00

标签: objective-c caching

我正在使用tableview显示视频,并希望将网址/数据保存在缓存中,以便用户在重播时不必再次下载相同的视频。

但是我想在用户退出应用之前保留它们。下次他打开应用程序时,他必须再次下载它们,所以我只需要将它们保存到应用程序的有效生命周期内。

我是否仍然应该进行缓存,或者是否有更有效的方法来执行此操作?

-(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]];}

1 个答案:

答案 0 :(得分:0)

AVFoundation允许在磁盘上传输视频文件。 我建议您考虑将视频保存在Documents文件夹或Caches文件夹中,然后从那里将其传输到applicationWillTerminate。然后,当用户退出应用时,系统会调用MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: URL-TO-FILE-ON-DISK]; [player prepareToPlay]; [player.view setFrame: myView.bounds]; // player's frame must match parent's [myView addSubview: player.view]; // ... [player play]; ,您可以从磁盘上擦除视频。

编辑

使用MPMoviePlayerController:

BEGIN {
    @ARGV = map glob("\"$_\""), @ARGV;
}

s/a/b/g;
s/c/d/g;
s/e/f/g;