iOS個人封裝圖片下載:Block與Delegate兩種方式

iOS應用程序開發中,不少地方要用到圖片下載的地方,爲了不引入SDWebImage第三方還能達到目的,只能自己寫一個類,用來下載圖片,在此用兩種方式:Block與Delegate,此中有任何不足,望大家指出,好了,直接上代碼。

1.Block方式下載圖片

//XQImageDownLoad.h:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
//block的參數是向外界返回的值
typedef void(^XQBlock)(UIImage *image);

@interface XQImageDownLoad : NSObject
#pragma mark - 向外界提供的接口
//第一個參數:圖片的URL;第二個參數:block回調
//初始化:通過block傳值,傳入URLstring,返回UIImage
- (instancetype)initWithString:(NSString *)URLstring block:(XQBlock)block;

//開始下載圖片
- (void)startDownLoadImage;
@end
//XQImageDownLoad.m:

#import "XQImageDownLoad.h"
//延展:用來添加兩個存儲外界傳來的值得屬性
@interface XQImageDownLoad()
@property(nonatomic,strong)NSString *string;
@property(nonatomic,copy)XQBlock block;
@end

@implementation XQImageDownLoad
//實現接口方法,同時取到外界傳入的值並存爲自己的私有屬性
- (instancetype)initWithString:(NSString *)URLstring block:(XQBlock)block
{
    self = [super init];
    if (self) {
        self.string = URLstring;
        self.block = block;
    }
    return self;
}
//實現開始下載圖片方法
- (void)startDownLoadImage
{
    //異步下載圖片
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.string]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
    {
        //多線程
        //回到主線程並把數據傳給外界
        [self performSelectorOnMainThread:@selector(mainThread:) withObject:data waitUntilDone:YES];
    }];
    [task resume];
}
//data轉換爲UIImage,並傳值給block
- (void)mainThread:(NSData *)data{
    UIImage *image = [UIImage imageWithData:data];
    //調用外界傳來的block,把需要傳的數據通過外界block傳出去
    self.block(image);
}

@end

2.Delegate方式下載圖片

//XQImageDownLoadDelegate.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
//協議
@protocol XQdelegate <NSObject>
//方法用於把最終數據(下載的圖片)傳到外界
- (void)XQImageDownDidFinishImage:(UIImage *)image;
@end

@interface XQImageDownLoadDelegate : NSObject
//給外界使用的接口:第一個參數爲圖片URL,第二個參數爲遵循協議的代理
-(instancetype)initWithURLString:(NSString *)URLString delegate:(id<XQdelegate>)delegate;
//開始下載圖片
- (void)startDownLoadImage;
@end
//XQImageDownLoadDelegate.m

#import "XQImageDownLoadDelegate.h"
//延展
@interface XQImageDownLoadDelegate ()
//兩個私有屬性
@property(nonatomic,weak)id<XQdelegate>delegate;
@property(nonatomic,strong)NSString *string;
@end

@implementation XQImageDownLoadDelegate
//實現接口方法,並把傳入的值賦給自己的私有屬性,便於內部使用
-(instancetype)initWithURLString:(NSString *)URLString delegate:(id<XQdelegate>)delegate{
    self = [super init];
    if (self) {
        self.string = URLString;
        self.delegate = delegate;
    }
    return self;
}
//實現開始下載圖片的方法
- (void)startDownLoadImage
{
    //異步下載圖片
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.string]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //返回主線程中把數據傳出去
        [self performSelectorOnMainThread:@selector(mainThread:) withObject:data waitUntilDone:YES];
    }];
    [task resume];
}
- (void)mainThread:(NSData *)data{
    UIImage *image = [UIImage imageWithData:data];
    //用外界傳入的代理調用協議方法,從而實現在外界實現協議方法時獲取到需要的數據
    [self.delegate XQImageDownDidFinishImage:image];
}

@end

這是我設計的簡單界面,點擊開始加載圖片開始加載:

這是我設計的簡單界面,點擊開始加載圖片開始加載

去控制器調用兩個方法:

//ViewController.m中的調用

//點擊按鈕時調用的方法
- (IBAction)ButtonAction:(id)sender {
#pragma mark - 封裝block方法
 /*
 此處WWW知識我隨意找的一張圖片,並將鏈接宏定義
 #define WWW @"http://img02.tooopen.com/images/20151229/tooopen_sy_153068856883.jpg"
 */
 //block中不要使用self,防止循環引用
    __weak typeof(self)weakSelf = self;

    XQImageDownLoad *xqImage = [[XQImageDownLoad alloc]initWithString:WWW block:^(UIImage *image) {

        weakSelf.imV.image = image;
    }];

    [xqImage startDownLoadImage];

#pragma mark - 封裝delegate方法
//記住遵循代理<XQdelegate>
    XQImageDownLoadDelegate *xqImage = [[XQImageDownLoadDelegate alloc]initWithURLString:WWW delegate:self];
    [xqImage startDownLoadImage];
}
//XQdelegate中的方法
- (void)XQImageDownDidFinishImage:(UIImage *)image{
    self.imV.image = image;
}

結果截圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章