//
// ViewController.m
// Lesson18_图片下载
//
// Created by Floating_SH on 15/12/11.
// Copyright © 2015年 SH. All rights reserved.
//
#import "ViewController.h"
#import "SHImageDownloader.h"
@interface ViewController ()<SHImageDownloaderDelegate>
/**
* 图片
*/
@property (weak, nonatomic) IBOutlet UIImageView *Picture;
@property (nonatomic,strong) UIActivityIndicatorView *activityIV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.activityIV = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIV.center = self.view.center;
self.activityIV.color = [UIColor orangeColor];
}
//#define pictureURL @"http://a3.qpic.cn/psb?/78ad28e3-5200-49a2-8331-85fc28eabc16/a4ZTAGvIXIAi2oEIkRPXY1wUL4GVUs0VJHSuJI.xhRM!/m/dHIBAAAAAAAA&bo=wAMbAgAAAAAFB*4!&rf=photolist"
#define pictureURL @"http://hn.lanou3g.com/uploadfile/2015/0109/20150109094909347.jpg"
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//代理完成图片下载
// [SHImageDownloader downloadImageWithURLString:pictureURL delegate:self];
//block完成图片下载
[SHImageDownloader downloadImageWithURLString:pictureURL block:^(UIImage * _Nonnull image) {
//这里block将image传递过来赋给self.Picture
self.Picture.image = image;
}];
}
//代理方法
- (void)imageDownloaderDidFinishedLoadedImage:(UIImage *)image{
self.Picture.image = image;
}
//主线程执行的方法
-(void)showPicture:(UIImage *)image{
self.Picture.image = image;
//停止ActivityIndicator动画并移除视图
[self.activityIV stopAnimating];
[self.activityIV removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// SHImageDownloader.h
// Lesson18_图片下载
//
// Created by Floating_SH on 15/12/11.
// Copyright © 2015年 SH. All rights reserved.
//
/**
* 图片下载类
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol SHImageDownloaderDelegate <NSObject>
/**
* 图片下载完成以后代理对象执行的方法,用于携带下载完成的数据,传递给外界
*
* @param image 下载完成的图片
*/
- (void)imageDownloaderDidFinishedLoadedImage:(nonnull UIImage *)image;
@end
/**
* 定义一个参数为image的block,用于回调
*
* @param image 传递的image参数
*/
typedef void(^SHDownloaderBlock)(UIImage * __nonnull image);
@interface SHImageDownloader : NSObject
/**
* 使用代理下载图片的方法
*
* @param urlString 图片网址字符串对象
* @param delegate 需要设置的代理对象
*/
+ (void)downloadImageWithURLString:(nonnull NSString *)urlString delegate:(nonnull id<SHImageDownloaderDelegate>)delegate;
/**
* 使用block下载图片
*
* @param urlString 图片网址字符串对象
* @param block 需要回调的block块
*/
+ (void)downloadImageWithURLString:(nonnull NSString *)urlString block:(SHDownloaderBlock __nonnull)block; //这的block带有一个参数(UIImage *image)
@end
//
// SHImageDownloader.m
// Lesson18_图片下载
//
// Created by Floating_SH on 15/12/11.
// Copyright © 2015年 SH. All rights reserved.
//
#import "SHImageDownloader.h"
@implementation SHImageDownloader
+ (void)downloadImageWithURLString:(nonnull NSString *)urlString delegate:(nonnull id<SHImageDownloaderDelegate>)delegate{
//需要判断传进来得字符串(截取字符串前四个字符是否等于HTTP)
NSURLSessionDownloadTask *downloadTask = [[NSURLSession sharedSession] downloadTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
//必须保证代理对象不为空并且代理对象实现代理方法的前提下,才传递数据给外界
if (delegate && [delegate respondsToSelector:@selector(imageDownloaderDidFinishedLoadedImage:)]) {
//让代理方法在主线程中执行,保证UI显示无误(设置主线程的时候要分情况)
dispatch_async(dispatch_get_main_queue(), ^{
[delegate imageDownloaderDidFinishedLoadedImage:image];
});
}
}];
[downloadTask resume];
}
+ (void)downloadImageWithURLString:(nonnull NSString *)urlString block:(nonnull SHDownloaderBlock)block{//SHDownloaderBlock类型的block带有一个参数(UIImage *image)
NSString *string = [urlString substringToIndex:4];
if ([string isEqualToString:@"http"]) {
NSURLSessionDownloadTask *downloadTask = [[NSURLSession sharedSession] downloadTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
dispatch_async(dispatch_get_main_queue(), ^{
//图片下载完成后,在主线程中使用block传递给外界
block(image);
});
}];
[downloadTask resume];
}else{
/**
* name:崩溃名字 reason:崩溃原因
*/
//抛出异常
@throw [NSException exceptionWithName:@"SHImageDownloader Error" reason:@"你瞎啊,你们家网址长这样?" userInfo:nil];
}
}
@end