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

查看:148
本文介绍了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年)

这篇文章了解 NSURLCache

This article about NSURLCache

SDWebImage (看起来不错,但只能用影像作品)

SDWebImage (looks great but only works with images)

AFDownloadRequestOperation

这似乎是一个pretty常见的情况,所以我真正需要的最佳做法,或例如code引用。

This seems like a pretty common use case, so I'm really looking for best practices and or references to example code.

推荐答案

这是非常简单的下载和高速缓存。下面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的使用懒惰图像DECOM pression方案,以便有延迟。这将成为锯齿滚动,如果你把这些图像转换成的tableView细胞。正确的解决办法是在背景图片DECOM preSS(或德code),然后在主线程显示DECOM pressed形象。

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.

要了解更多关于懒惰的图像DECOM pression,看到以下内容:<一href=\"http://www.cocoanetics.com/2011/10/avoiding-image-decom$p$pssion-sickness/\">http://www.cocoanetics.com/2011/10/avoiding-image-decom$p$pssion-sickness/

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

我的建议是使用SDWebImage为您的图片,并在上面code为您的视频。

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

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

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