Vai*_*eri 5 xcode caching objective-c ios
有没有办法将视频保存在缓存中,然后在需要时将其恢复?
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
});
Run Code Online (Sandbox Code Playgroud)
我在Stack Overflow上找到了这个代码,但它似乎不起作用.
您可以用于NSURLSessionDownloadTask下载视频或图像,直接写入临时目录。
\n\n\n下载期间,会话定期调用 delegate\xe2\x80\x99s\n
\n\nURLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:\n 方法并提供状态信息。完成后,会话调用 delegate\xe2\x80\x99s
\nURLSession:downloadTask:didFinishDownloadingToURL:\n 方法或完成处理程序。在该方法中,您必须打开文件进行读取,或者将其移动到 app\xe2\x80\x99s 沙箱容器目录中的永久位置。
NSURL *URL = [NSURL URLWithString:@"http://blog.jimdo.com/wp-content/uploads/2014/01/tree-247122.jpg"];\nNSURLRequest *request = [NSURLRequest requestWithURL:URL];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request\n completionHandler:\n^(NSURL *location, NSURLResponse *response, NSError *error) {\n NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];\n NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];\n NSURL *documentURL = [documentsDirectoryURL URLByAppendingPathComponent:[response \n suggestedFilename]];\n [[NSFileManager defaultManager] moveItemAtURL:location\n toURL:documentURL\n error:nil];\n }];\n\n [downloadTask resume];\nRun Code Online (Sandbox Code Playgroud)\n\n\n
| 归档时间: |
|
| 查看次数: |
4812 次 |
| 最近记录: |