IOS 如何异步下载和缓存图像和视频以在我的应用程序中使用 [英] IOS How do I asynchronously download and cache images and videos for use in my app

查看:23
本文介绍了IOS 如何异步下载和缓存图像和视频以在我的应用程序中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iphone 应用程序,可以同时显示图像和视频.该应用程序的大部分图像和视频的结构方式将保持不变,偶尔会添加一个.我想就异步下载和缓存图像和视频的最佳和最简单的方法提出意见,以便即使在应用程序退出后它们也能持续存在.另外,我只真正关心 IOS 5 及更高版本.

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.

这篇文章关于异步图片缓存(旧 2009)

This article about asynchronous image caching (old 2009)

这篇文章关于 NSURLCache

This article about NSURLCache

SDWebImage(看起来不错,但只适用于图片)

SDWebImage (looks great but only works with images)

AFDownloadRequestOperation

这似乎是一个非常常见的用例,所以我真的在寻找最佳实践和/或对示例代码的引用.

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

现在 UIImages 有一些特别之处,使得像 SDWebImage 这样的库变得如此有价值,即使异步下载图像如此简单.当您显示图像时,iOS 使用延迟图像解压缩方案,因此存在延迟.如果将这些图像放入 tableView 单元格中,这将变得锯齿状滚动.正确的解决方法是在后台进行图像解压(或解码),然后在主线程中显示解压后的图像.

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.

要了解有关延迟图像解压缩的更多信息,请参阅:http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/

To read more about lazy image decompression, see this: http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/

我的建议是对图片使用 SDWebImage,对视频使用上面的代码.

My advice is to use SDWebImage for your images, and the code above for your videos.

这篇关于IOS 如何异步下载和缓存图像和视频以在我的应用程序中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆