如何异步下载1000张图片? [英] How to asynchronously download 1000 images?

查看:144
本文介绍了如何异步下载1000张图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从URL下载了大约1000张图像到iPhone文件系统,但我想异步进行。目前我正在使用此代码:

I downloaded around 1000 images from URLs to the iPhone file system, but I'd like to do it asynchronously. Currently I'm using this code:

-(void)saveImageInFile:(NSString *)imageName image:(UIImage *)img{

    // add the images in the file
    NSData *tmpData;

    if ([self.constImageType isEqualToString:@"png"]) {
        tmpData = UIImagePNGRepresentation (img);
    }

    else if ([self.constImageType isEqualToString:@"jpg"]) {
        tmpData = UIImageJPEGRepresentation(img, 0.7f);
    }

    NSString *path = [self.documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName,self.constImageType]];
    //NSLog(path);
    [tmpData writeToFile:path atomically:YES];

}


推荐答案

我是使用1000以上的图片做类似的事情。我在核心数据中存储图像的url,然后获取记录属性<$的所有记录的 NSArray c $ c> imageDownloaded = NO 。在下载图像时,我设置了该记录的 imageDownloaded = YES ,所以如果应用程序需要重新启动,它可以从中断处继续。

I am doing something similar with a lot more images than 1000. I am storing the url's for the images in core data and then getting an NSArray of all the records where the record attribute imageDownloaded = NO. While downloading the images I set that record's imageDownloaded = YES so if the app needs to restart it can pick up where it left off.

这是一个发生什么事的小例子。我正在使用Grand Central Dispatch来处理这个过程,所以我没有阻止主线程。

Here is a small example of whats happening. I am using Grand Central Dispatch to background this process so I am not blocking the main thread.

__block NSArray *records; //...get records

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0];

        for (NSManagedObject *obj in records) {
            NSString *imageName = [obj valueForKey:@"filename"];

            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[obj valueForKey:@"remote_path"],imageName]];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
            NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
            [responseData writeToFile:[NSString stringWithFormat:@"%@/%@",documentsDirectory,imageName] atomically:NO];
            [obj setValue:[NSNumber numberWithBool:YES] forKey:@"imageDownloaded"];
        }
    });

有关Grand Central Dispatch的更多信息
https://developer.apple.com/library/mac/#documentation/Performance/Reference/ GCD_libdispatch_Ref / Reference / reference.html

More Information on Grand Central Dispatch https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

这篇关于如何异步下载1000张图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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