使用GCD异步下载图片 可以中途停止下载

//

//  PlayGifViewController.h

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface PlayGifViewController : UIViewController

@end

//

//  PlayGifViewController.m

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//

#define Image_W 85

#define Image_H 110

#define kBorder 10

#import "PlayGifViewController.h"

@interface PlayGifViewController ()

@property (nonatomic, strong) NSArray * imageArr;

@end

@implementation PlayGifViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor blackColor];

    _imageArr = @[

                  @"http://g.hiphotos.baidu.com/image/pic/item/6a600c338744ebf8b470ca3bdbf9d72a6059a7b5.jpg",

                  @"http://e.hiphotos.baidu.com/image/pic/item/d058ccbf6c81800afd90790ab33533fa828b47b9.jpg",

                  @"http://a.hiphotos.baidu.com/image/pic/item/10dfa9ec8a1363270ab03637938fa0ec08fac751.jpg",

                  @"http://a.hiphotos.baidu.com/image/pic/item/21a4462309f7905258eb0e740df3d7ca7acbd580.jpg",

                  @"http://c.hiphotos.baidu.com/image/pic/item/14ce36d3d539b600d07655bfeb50352ac65cb77a.jpg",

                  @"http://c.hiphotos.baidu.com/image/pic/item/8435e5dde71190ef5c21f9d5cc1b9d16fcfa60e5.jpg",

                  @"http://a.hiphotos.baidu.com/image/pic/item/e4dde71190ef76c6b7c1b72b9f16fdfaaf51679a.jpg",

                  @"http://f.hiphotos.baidu.com/image/pic/item/a9d3fd1f4134970a15a4ab2197cad1c8a6865dec.jpg",

                  @"http://e.hiphotos.baidu.com/image/pic/item/58ee3d6d55fbb2fb0f5f85134d4a20a44623dcbf.jpg",

                  @"http://g.hiphotos.baidu.com/image/pic/item/eac4b74543a98226d934f9268882b9014a90eb3e.jpg",

                  @"http://c.hiphotos.baidu.com/image/pic/item/2cf5e0fe9925bc31c6abca915cdf8db1ca1370c1.jpg",

                  @"http://e.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee64d4c4102ff5e0fe98257ec1.jpg",

                  @"http://h.hiphotos.baidu.com/image/pic/item/63d9f2d3572c11df4eb5b586612762d0f703c22b.jpg"];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

    

    dispatch_async(queue, ^{

        for (int i = 0; i < _imageArr.count; i++) {

            [self uploadImage:_imageArr[i]];

        }

        

        dispatch_async(dispatch_get_main_queue(), ^{

            [self updateUI:_imageArr];

        });

    });

}

- (void)updateUI:(NSArray *)imageArr

{

    for (int i = 0; i < imageArr.count; i++) {

        

        UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake((i % 3) * (Image_W + (([UIScreen mainScreen].bounds.size.width - Image_W * 3) / 4.0f)) + (([UIScreen mainScreen].bounds.size.width - Image_W * 3) / 4.0f), (i / 3) * (Image_H + kBorder) + 74, Image_W, Image_H)];

        NSString * imageFilePath = NSHomeDirectory();

        imageFilePath = [imageFilePath stringByAppendingPathComponent:@"Documents/imagesFilePath/"];

        imageFilePath = [imageFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%i.jpg", i]];

        NSData * imageData = [NSData dataWithContentsOfFile:imageFilePath];

        if (imageData) {

            UIImage * image = [UIImage imageWithData:imageData];

            imageV.image = image;

        }

        

        [self.view addSubview:imageV];

    }

}

- (void)uploadImage:(NSString *)imageUrl

{

    NSString * path = NSHomeDirectory();

    path = [path stringByAppendingPathComponent:@"Documents/imagesFilePath"];

    NSFileManager * fm = [NSFileManager defaultManager];

    NSError * error = [[NSError alloc] init];

    [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];

    

    // 下载图片

    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];

    if (data) {

        static int i = 0;

        if (i == 10) {

            // 停止下载图片

            return;

        }

        NSString * filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%i.jpg", i++]];

        NSFileManager * fm = [NSFileManager defaultManager];

        [fm createFileAtPath:filePath contents:data attributes:nil];

    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    

}

@end

原文地址:https://www.cnblogs.com/hanzhuzi/p/4461804.html