Save video in cache

Is there a way to save the video in cache and then extract it if necessary?

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 }); 

I found this code in Stack Overflow, but it does not work.

+5
source share
2 answers

You can use NSURLSessionDownloadTask to load a video or image by directly writing to the temp directory.

During loading, the session periodically calls delegates URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite: method with status information.

Upon completion, the session calls the delegates URLSession:downloadTask:didFinishDownloadingToURL: method or completion handler. In this method, you must either open the file for reading or move it to a permanent location in the container directory for application sandboxes.

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

Class reference NSURLSessionDownloadTask

+3
source

NSCache is similar to NSDictionary, but empties itself if it detects lost memory. Here are the docs: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSCache_Class/

Even small videos are large, so they are likely to immediately unload it from memory or refuse to store it in the first place.

You can save the video in NSDictionary, which is stored in some singleton type, but this leads to the following problem: I can’t understand for what reason storing the video in memory for the iOS device. Not much memory is much smaller than a desktop computer, but all iOS devices have exceptionally fast disk I / O. That is, it is extremely important to cache video on an iOS device and does not provide a noticeable advantage.

So you can create a singleton, add a dictionary and save it there. But you really shouldn't: just remove it from disk when necessary.

0
source

All Articles