這個方法就是根據網絡下載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
{
}