IOS How do I asynchronously download and cache ima

2019-01-16 07:10发布

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)

AFDownloadRequestOperation

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

3条回答
祖国的老花朵
2楼-- · 2019-01-16 07:19

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.

查看更多
我命由我不由天
3楼-- · 2019-01-16 07:19
-(void)tableView:(UITableView *)tableView 
willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *view = (UIView *)cell;

    view.transform = CGAffineTransformMakeScale(0.0, 0.0);

    [UIView animateWithDuration:0.3 animations:^{
        view.transform = CGAffineTransformIdentity;
    }];
}
查看更多
Summer. ? 凉城
4楼-- · 2019-01-16 07:30

A strong iOS image cache component must:

  • download images asynchronously, so the main queue is used as little as possible
  • decompress images on a background queue. This is far from being trivial. See a strong article about background decompression
  • cache images into memory and on disk.Caching on disk is important because the app might be closed or need to purge the memory because of low memory conditions. In this case, re-loading the images from disk is a lot faster than downloading them. Note: if you use NSCache for the memory cache, this class will purge all it’s contents when a memory warning is issued. Details about NSCache here http://nshipster.com/nscache/
  • store the decompressed image on disk and in memory to avoid redoing the decompression
  • use GCD and blocks. This makes the code more performant, easier to read and write. In nowadays, GCD and blocks is a must for async operations
  • nice to have: category over UIImageView for trivial integration. nice to have: ability to process the image after download and before storing it into the cache.

ADVANCED IMAGING ON IOS


To find out more about imaging on iOS, how the SDK frameworks work (CoreGraphics, Image IO, CoreAnimation, CoreImage), CPU vs GPU and more, go through this great article by @rsebbe.

Source: https://bpoplauschi.wordpress.com/2014/03/21/ios-image-caching-sdwebimage-vs-fastimage/

查看更多
登录 后发表回答