imageDownloader
.h
#import <UIKit/UIKit.h>
@protocol imageDownloadDelegate <NSObject>
?
@optional
-(void)imageDownloadWithImage:(UIImage *)image;
?
@end
// 聲明一個block 參數類型是UIImage 返回值是void 別名Result
typedef void(^Result)(UIImage *img);
@interface ImageDownload : NSObject
?
#pragma mark - 聲明方法? 被調用后直接下載
+(void)imageDownloadWithUrlStr:(NSString *)urlStr
? ? ? ? ? ? ? ? ? ? ? delegate:(id<imageDownloadDelegate>)delegate;
?
#pragma mark - 聲明方法 使用block的方式
+(void)imageDownloadWithUrlStr:(NSString *)urlStr
? ? ? ? ? ? ? ? ? ? ? ? result:(Result)result;
?
@end
.m#import "ImageDownload.h"
?
@implementation ImageDownload
#pragma mark - 實現方法 代理
+(void)imageDownloadWithUrlStr:(NSString *)urlStr
? ? ? ? ? ? ? ? ? ? ? delegate:(id<imageDownloadDelegate>)delegate
{
? ? NSURL *url = [NSURL URLWithString:urlStr];
? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];
? ? [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue new]autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
? ? ? ? UIImage *image = [UIImage imageWithData:data];
? ? ? ? dispatch_sync(dispatch_get_main_queue(),^{
? ? ? ? ? ? if (delegate!=nil && [delegate respondsToSelector:@selector(imageDownloadWithImage:)]) {
? ? ? ? ? ? ? ? [delegate imageDownloadWithImage:image];
? ? ? ? ? ? }
? ? ? ? });
? ? }];
}
#pragma mark - 實現方法? block 方式
+(void)imageDownloadWithUrlStr:(NSString *)urlStr
? ? ? ? ? ? ? ? ? ? ? ? result:(Result)result
{
? ? NSURL *url = [NSURL URLWithString:urlStr];
? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];
? ? [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue new]autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
? ? ? ? UIImage *image = [UIImage imageWithData:data];
?? ? ? ?
? ? ? ? // 回到主線程使用block
? ? ? ? dispatch_sync(dispatch_get_main_queue(), ^{
?? ? ? ? ? // block 調用
? ? ? ? ? ? result(image);
? ? ? ? });
? ? }];
}
?
再去 ViewController?
- (IBAction)buttonAction:(UIButton *)sender
{
? ? /*
?? //http://img4.duitang.com/uploads/item/201208/10/20120810091225_hvA2r.thumb.700_0.jpeg
? ? //1 NSURL
? ? NSURL *url = [NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201208/10/20120810091225_hvA2r.thumb.700_0.jpeg"];
?? ?
? ? //2 NSURLREQUEST
? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];
?? ?
? ? //3 NSURLConnection
?? ?
? ? __block ViewController *weakSelf = self;
? ? [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue new]autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
? ? ? ? //4 獲取data數據? 轉為UIImage類型
? ? ? ? UIImage *image = [UIImage imageWithData:data];
? ? ? ? //5 回到主界面 刷新數據
? ? ? ? dispatch_sync(dispatch_get_main_queue(),^{
? ? ? ? ? ? weakSelf.imageView.image = image;
? ? ? ? });
? ? }];
?? ? */
? ? // 直接發送消息 設置代理
? ? // [ImageDownload imageDownloadWithUrlStr:@"http://img4.duitang.com/uploads/item/201208/10/20120810091225_hvA2r.thumb.700_0.jpeg" delegate:self];
?? ?
? ? // 使用block
? ? __block ViewController *weakSelf = self;
? ? [ImageDownload imageDownloadWithUrlStr:@"http://img4.duitang.com/uploads/item/201208/10/20120810091225_hvA2r.thumb.700_0.jpeg" result:^(UIImage *img)
?? ? {
?? ? ? ? // 將block中參數顯示到UIImageView上
?? ? ? ? weakSelf.imageView.image = img;
?? ? }];
?
}
?
@end
?
轉載于:https://www.cnblogs.com/masami521/p/4724364.html
總結
以上是生活随笔為你收集整理的imageDownloader的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么会一直梦到前任
- 下一篇: maven详解之坐标与依赖