加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 在缓存中保存视频

发布时间:2020-12-14 19:24:58 所属栏目:百科 来源:网络整理
导读:有没有办法将视频保存在缓存中,然后在需要时将其恢复? NSCache *memoryCache; //assume there is a memoryCache for images or videosNSString *urlString = @"http://blog.jimdo.com/wp-content/uploads/2014/01/tree-247122.jpg";NSString *path = [[NSBu
有没有办法将视频保存在缓存中,然后在需要时将其恢复?

NSCache *memoryCache; //assume there is a memoryCache for images or videos
NSString *urlString = @"http://blog.jimdo.com/wp-content/uploads/2014/01/tree-247122.jpg";
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"A"] ofType:@"mov"];
NSURL *url = [NSURL fileURLWithPath:path];

NSData *downloadedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),^
{
    if (downloadedData)
    {
        // STORE IN FILESYSTEM
        NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) lastObject];
        NSString *file = [path stringByAppendingPathComponent:@"B.mov"];
        [downloadedData writeToFile:file options:NSDataWritingAtomic error:nil];
        NSLog(@"Cache File : %@",file);

        // STORE IN MEMORY
        [memoryCache setObject:downloadedData forKey:@"B.mov"];
    }
    // NOW YOU CAN CREATE AN AVASSET OR UIIMAGE FROM THE FILE OR DATA

});

我在Stack Overflow上找到了这个代码,但它似乎不起作用.

解决方法

您可以使用NSURLSessionDownloadTask下载视频或图像,直接写入临时目录.

During download,the session periodically calls the delegate’s
URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:
method with status information.

Upon completion,the session calls the delegate’s URLSession:downloadTask:didFinishDownloadingToURL:
method or completion handler. In that method,you must either open the file for
reading or move it to a permanent location in your app’s sandbox container directory.

NSURL *URL = [NSURL URLWithString:@"http://blog.jimdo.com/wp-content/uploads/2014/01/tree-247122.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request
                                                     completionHandler:
^(NSURL *location,NSURLResponse *response,NSError *error) {
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES) firstObject];
    NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];
    NSURL *documentURL = [documentsDirectoryURL URLByAppendingPathComponent:[response 
    suggestedFilename]];
    [[NSFileManager defaultManager] moveItemAtURL:location
                                            toURL:documentURL
                                            error:nil];
  }];

  [downloadTask resume];

NSURLSessionDownloadTask Class Reference

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读