在缓存中保存视频

Save a video in Cache

有没有办法将视频保存在高速缓存中,然后在需要时检索它?

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, NSUserDomainMask, 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

NSCache 类似于 NSDictionary,但如果它检测到浪费的内存,则会自行清空。以下是文档:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSCache_Class/

即使是小视频也很大,所以它可能会立即从内存中转储或拒绝存储。

您可以将视频存储在存储在某种类型的单例中的 NSDictionary 中,但这会导致下一个问题:我无法理解为 iOS 设备将视频存储在内存中的任何理由.内存很少——比台式计算机少得多——但所有 iOS 设备都具有非常快的磁盘 I/O。也就是说,在 iOS 设备上缓存视频非常昂贵,而且没有任何明显的好处。

因此,您可以创建一个单例,添加一个字典,并将其存储在那里。但你真的不应该:只在需要时从磁盘中检索它。