封装一个 图片异步下载类

来源:互联网 发布:网络教育是全日制吗 编辑:程序博客网 时间:2024/05/21 17:01

这个方法就是根据网络下载get 异步方法来实现的

get异步代理方法

1.创建一个网址对象    对象是根据字符串来创立的

<span style="font-size:24px;">NSURL *url = [NSURL URLWithString:str];</span>
2.根据网址对象  创建一个可变请求

<span style="font-size:24px;">NSMutableURLRequest *request = [NSMutableURLRequest  requestWithURL:url  cachePolicy:(NSURLRequestUseProtocolCachePolicy)  timeoutInterval:30];</span>

[request setHTTPMethod:@"Get"];

3.创建链接(异步代理方法)

self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

 [self.connection start];


ImageDownLoader.h

#import <Foundation/Foundation.h>

/*
 本类可以异步加载图片
 
 */

//创建一个协议
@protocol ImageDownLoaderDelegate <NSObject>

//请求成功
- (void)imageDownLoaderSucceedWithData:(NSData *)data;
//请求失败
- (void)imageDownLoaderFailedWithData:(NSError *)error;

@end

@interface ImageDownLoader : NSObject
// 链接
@property (nonatomic,retain)NSURLConnection *connection;
// data
@property (nonatomic,retain)NSMutableData *data;
// 声明代理对象
@property (nonatomic,assign)id <ImageDownLoaderDelegate> delegate;


//初始化方法(有需要写 请求中 可能遇到的参数 -- 网址)
- (instancetype)initWithUrl:(NSString *)url delegate:(id <ImageDownLoaderDelegate>) delegate;

//开始方法
- (void)imageDownLoaderStart;
//终止方法
- (void)imageDownLoaderCancel;

@end

ImageDownLoader.m

#import "ImageDownLoader.h"

@interface ImageDownLoader ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>

@end

//图片一般get请求
@implementation ImageDownLoader

- (void)dealloc
{
    
    [_connection release];
    [_data release];
    [super dealloc];
}

- (instancetype)initWithUrl:(NSString *)url delegate:(id<ImageDownLoaderDelegate>)delegate
{
    self = [super init];
    if (self) {
        //设置代理
        self.delegate = delegate;
        
        //创建网址对象
        NSURL *uurl = [NSURL URLWithString:url];
        //创建请求
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:uurl cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30];
        
        [request setHTTPMethod:@"Get"];
        //创建链接(异步代理方法)
        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
        
        [self.connection start];
    }
    return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.data = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.data appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //图片请求完成
    if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownLoaderSucceedWithData:)]) {
        
        //代理调用
        [_delegate imageDownLoaderSucceedWithData:self.data];
        
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownLoaderFailedWithData:)]) {
        [_delegate imageDownLoaderFailedWithData:error];
    }
}

//开始方法
- (void)imageDownLoaderStart
{
    [self.connection start];
}
//终止方法
- (void)imageDownLoaderCancel
{
    [self.connection cancel];
}

@end


在RootViewController.m上

#import "RootViewController.h"
#import "ImageDownLoader.h"

#define kImageUrl @"http://img3.douban.com/view/event_poster/median/public/10f53a2ad8b38c5.jpg"
@interface RootViewController ()<ImageDownLoaderDelegate>
@property (nonatomic ,retain)UIImageView *imageView;

@property (nonatomic ,retain)ImageDownLoader *imageDownLoader;

@end

@implementation RootViewController

- (void)dealloc
{
    //当页面被销毁时 终止请求
    [_imageDownLoader imageDownLoaderCancel];
    [_imageDownLoader release];
    [_imageView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.imageView];
    [_imageView release];
    
    //创建请求对象
   self.imageDownLoader = [[ImageDownLoader alloc] initWithUrl:kImageUrl delegate:self];
    //开始请求
    //[imageDownLoader imageDownLoaderStart];
    [_imageDownLoader release];
    
}

//请求成功
- (void)imageDownLoaderSucceedWithData:(NSData *)data
{
    self.imageView.image = [UIImage imageWithData:data];
    NSLog(@"%@",data);
}
//请求失败
- (void)imageDownLoaderFailedWithData:(NSError *)error
{
    
}






0 0
原创粉丝点击