//
// AppDelegate.h
// UI17_imageDownLoader
//
// Created by l on 15/9/23.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
// UI17_imageDownLoader
//
// Created by l on 15/9/23.
// Copyright (c) 2015年 . All rights reserved.
//
#import "AppDelegate.h"
#import "ImageDownloader.h"
@interface AppDelegate ()<ImageDownloaderDelegate>
@property (nonatomic, retain) UIImageView *imageView;
@end
@implementation AppDelegate
- (void)dealloc{
[_window release];
[_imageView release];
[super dealloc];
}
//实现自定义协议方法
- (void)imageDownloader:(ImageDownloader *)downloader didFinishLoading:(UIImage *)image{
_imageView.image = image;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//一. 异步图片下载类
_imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(80, 100, 250, 300))];
//图片同步下载: (同步网络请求,会卡线程)
// _imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://picm.photophoto.cn/015/037/003/0370032259.jpg"]]];
//--------------//
//代理方法:
[ImageDownloader imageDownloaderWithURLString:@"http://d.hiphotos.baidu.com/image/pic/item/d0c8a786c9177f3ec4c5de0c72cf3bc79f3d5639.jpg" delegate:self];
//-------------//
//block:
// NSString *url = @"http://img.sootuu.com/vector/2006-4/200642011196982.jpg";
// [ImageDownloader sendAsynchonousURLString:url completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//
// //获取到回调的data数据,赋值给imageView.image
// _imageView.image = [UIImage imageWithData:data];
// }];
//
//
//加载_imageView
[self.window addSubview:_imageView];
return YES;
}
@end
//
// ImageDownloader.h
// UI17_imageDownLoader
//
// Created by l on 15/9/23.
// Copyright (c) 2015年 . All rights reserved.
//此为异步图片下载类,封装了系统的connection , 异步网络请求.
//采用delegate 和 block 两种方式.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
//定义block类型
typedef void (^CompletionHandler)(NSURLResponse *response, NSData *data, NSError *error);
@class ImageDownloader;
//定义协议
@protocol ImageDownloaderDelegate <NSObject, NSURLConnectionDelegate, NSURLConnectionDataDelegate>
@optional
//图片下载完成之后触发
- (void)imageDownloader:(ImageDownloader *)downloader didFinishLoading:(UIImage *)image;
//图片下载失败的时候触发
- (void)imageDownloader:(ImageDownloader *)downloader didFailWithError:(NSError *)error;
@end
@interface ImageDownloader : NSObject
@property (nonatomic, copy) NSString *urlString;//图片网络地址
//1.delegate
@property (nonatomic, assign) id<ImageDownloaderDelegate> delegate;
//自定义初始化方法
- (instancetype)initWithURLString:(NSString *)urlString delegate:(id<ImageDownloaderDelegate>) delegate;
//便利构造器
+ (ImageDownloader *)imageDownloaderWithURLString:(NSString *)urlString delegate:(id<ImageDownloaderDelegate>) delegate;
+ (void)sendAsynchonousURLString:(NSString *)urlString completionHandler:(CompletionHandler)handler;
@end
//
// ImageDownloader.m
// UI17_imageDownLoader
//
// Created by l on 15/9/23.
// Copyright (c) 2015年 . All rights reserved.
//
#import "ImageDownloader.h"
@interface ImageDownloader ()<NSURLConnectionDelegate, NSURLConnectionDataDelegate>
@property (nonatomic, retain) NSMutableData *receiveData;
@end
@implementation ImageDownloader
- (void)dealloc{
[_urlString release];
[_receiveData release];
[super dealloc];
}
//重写初始化方法(自定义初始化方法)
- (instancetype)initWithURLString:(NSString *)urlString delegate:(id<ImageDownloaderDelegate>) delegate{
self = [super init];
if (self) {
self.urlString = urlString;
self.delegate = delegate;
//进行网络请求
NSURL *url = [NSURL URLWithString:_urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:0 timeoutInterval:10];
//异步网络请求方式,代理
[NSURLConnection connectionWithRequest:request delegate:self];
}
return self;
}
//便利构造器
+ (ImageDownloader *)imageDownloaderWithURLString:(NSString *)urlString delegate:(id<ImageDownloaderDelegate>) delegate{
return [[[ImageDownloader alloc] initWithURLString:urlString delegate:delegate] autorelease];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
self.receiveData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receiveData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//数据接收完成后,把数据通过协议的方法,作为方法参数,传递给delegate
UIImage *image = [UIImage imageWithData:self.receiveData];
if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownloader:didFinishLoading:)]) {
[_delegate imageDownloader:self didFinishLoading:image];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownloader:didFailWithError:)]) {
[_delegate imageDownloader:self didFailWithError:error];
}
}
//block
+ (void)sendAsynchonousURLString:(NSString *)urlString completionHandler:(CompletionHandler)handler{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//把参数通过block的参数传递出去
handler(response, data, connectionError);
}];
}
@end
iOS编程------UITableView中图片的异步下载 / KVO监测Model图片下载
最新推荐文章于 2016-06-03 21:56:00 发布