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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值