Có cách nào để lưu video trong Bộ nhớ Cache và sau đó thử lại khi cần?Lưu video trong Bộ nhớ 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
});
Tôi tìm thấy mã này trên Stack Overflow, nhưng nó dường như không hoạt động.
Op, bạn có câu trả lời? – Pawan
Vẫn còn nhầm lẫn với hành vi của Thư mục Cache –
Có phải nội dung của Danh mục Bộ nhớ cache sẽ bị xóa nếu ứng dụng bị chấm dứt không? –