IOS如何异步下载和缓存图像和视频以供我的应用程序使用 [英] IOS How do I asynchronously download and cache images and videos for use in my app
问题描述
以下是我迄今为止发现的一些信息,但我仍然不清楚最好的方法是什么,并且如果缓存将持续存在。
本文关于异步图像缓存(旧2009)
本文关于NSURLCache
SDWebImage (看起来不错,但只适用于图像)
这似乎是一个很常见的用例,所以我真的在寻找最佳实践和对示例代码的引用
下载和缓存非常简单。以下代码将异步下载和缓存。
NSCache * memoryCache; //假设有图像或视频的memoryCache
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),^ {
NSString * urlString = @http:// URL ;
NSData * uploadedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
if(downloadedData){
// STORE IN FILESYSTEM
NSString * cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString * file = [cachesDirectory stringByAppendingPathComponent:urlString];
[downloadedData writeToFile:file atomically:YES ];
//存储在内存
[memoryCache setObject:downloadedData forKey:urlString];
}
//现在你可以创建AVASSET或从文件或数据中获取$ $ $ $ $ $ $ $ $
现在有一些特殊的UIImages,使像SDWebImage这样的图书馆非常有价值,即使异步下载图像很容易。当您显示图像时,iOS使用懒惰图像解压缩方案,因此存在延迟。如果将这些图像放入tableView单元格,这将变成锯齿状的滚动。正确的解决方案是在后台进行图像解压缩(或解码),然后在主线程中显示解压缩的图像。
要了解有关懒惰图像解压缩的更多信息,请参阅: http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/
我的建议是为您的图像使用SDWebImage,并为您的视频使用上面的代码。
I have an iphone application that displays both images and videos. The way the app is structured most of the images and videos will remain the same, with one occasionally added. I would like an opinion on the best and easiest method for asynchronously downloading and caching both images and videos, so that they will persist even after the application has quit. Also, I am only really concerned with IOS 5 and later.
Here is some information I have found thus far, but I am still unclear about what the best method is, and if the cache will be persistent.
This article about asynchronous image caching (old 2009)
This article about NSURLCache
SDWebImage (looks great but only works with images)
This seems like a pretty common use case, so I'm really looking for best practices and or references to example code.
It is very simple to download and cache. The following code will asynchronously download and cache.
NSCache *memoryCache; //assume there is a memoryCache for images or videos
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSString *urlString = @"http://URL";
NSData *downloadedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
if (downloadedData) {
// STORE IN FILESYSTEM
NSString* cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *file = [cachesDirectory stringByAppendingPathComponent:urlString];
[downloadedData writeToFile:file atomically:YES];
// STORE IN MEMORY
[memoryCache setObject:downloadedData forKey:urlString];
}
// NOW YOU CAN CREATE AN AVASSET OR UIIMAGE FROM THE FILE OR DATA
});
Now there is something peculiar with UIImages that makes a library like SDWebImage so valuable , even though the asynchronously downloading images is so easy. When you display images, iOS uses a lazy image decompression scheme so there is a delay. This becomes jaggy scrolling if you put these images into tableView cells. The correct solution is to image decompress (or decode) in the background, then display the decompressed image in the main thread.
To read more about lazy image decompression, see this: http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/
My advice is to use SDWebImage for your images, and the code above for your videos.
这篇关于IOS如何异步下载和缓存图像和视频以供我的应用程序使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!