iOS應用程序開發中,不少地方要用到圖片下載的地方,爲了不引入SDWebImage第三方還能達到目的,只能自己寫一個類,用來下載圖片,在此用兩種方式:Block與Delegate,此中有任何不足,望大家指出,好了,直接上代碼。
1.Block方式下載圖片
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef void(^XQBlock)(UIImage *image);
@interface XQImageDownLoad : NSObject
#pragma mark - 向外界提供的接口
- (instancetype)initWithString:(NSString *)URLstring block:(XQBlock)block;
- (void)startDownLoadImage;
@end
#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];
}
- (void)mainThread:(NSData *)data{
UIImage *image = [UIImage imageWithData:data];
self.block(image);
}
@end
2.Delegate方式下載圖片
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol XQdelegate <NSObject>
- (void)XQImageDownDidFinishImage:(UIImage *)image;
@end
@interface XQImageDownLoadDelegate : NSObject
-(instancetype)initWithURLString:(NSString *)URLString delegate:(id<XQdelegate>)delegate;
- (void)startDownLoadImage;
@end
#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
這是我設計的簡單界面,點擊開始加載圖片開始加載:
去控制器調用兩個方法:
- (IBAction)ButtonAction:(id)sender {
#pragma mark - 封裝block方法
__weak typeof(self)weakSelf = self;
XQImageDownLoad *xqImage = [[XQImageDownLoad alloc]initWithString:WWW block:^(UIImage *image) {
weakSelf.imV.image = image;
}];
[xqImage startDownLoadImage];
#pragma mark - 封裝delegate方法
XQImageDownLoadDelegate *xqImage = [[XQImageDownLoadDelegate alloc]initWithURLString:WWW delegate:self];
[xqImage startDownLoadImage];
}
- (void)XQImageDownDidFinishImage:(UIImage *)image{
self.imV.image = image;
}
結果截圖: