//
// RootViewController.m
// ImageDownLoader-01
//
// Created by WDX on 15/9/14.
// Copyright (c) 2015年 WDongXu. All rights reserved.
#import "RootViewController.h"
// 加载图片
// 一,获取照片
#define kImageUrl @"http://img3.douban.com/view/event_poster/median/public/10f53a2ad8b38c5.jpg"
@interface RootViewController ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate> // 遵守协议
//声明一个可变的data接受收据
@property (nonatomic, retain)NSMutableData *data;
// 链接声明 方便终止请求
@property (nonatomic, retain)NSURLConnection *connection;
// 创建个ImageView属性 下面的修改一下,用属性表示 self.
@property (nonatomic, retain)UIImageView *imageView;
@end
@implementation RootViewController
- (void)dealloc
{
[_connection cancel];
[_data release];
[_connection release];
[_imageView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setUpData];
self.imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
self.imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.imageView];
[_imageView release];
}
// 二,异步请求一张图片
- (void)setUpData
{
// 1,创建一个NSURL
NSURL *url = [NSURL URLWithString:kImageUrl];
// 2, 用url对象创建一个请求
NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30.0];
[requst setHTTPMethod:@"GET"];
// 3, 用请求 创建一个链接 使用代理方法
self.connection = [NSURLConnection connectionWithRequest:requst delegate:self];
// 4,开启链接
[self.connection start];
}
// 代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// 初始化data
self.data = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 拼接data
[self.data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// 加载完成
self.imageView.image = [UIImage imageWithData:self.data];
// ***************写入沙盒***************************
// 写入data 起一个名字 newImage.png
NSArray *documentsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = documentsArr[0];
// 拼接路径
NSString *imagePath = [documents stringByAppendingPathComponent:@"newImage.png"];
// 把data写入
[self.data writeToFile:imagePath atomically:YES];
NSLog(@"%@", imagePath);
}
#import <Foundation/Foundation.h>
// 1,创建协议
@protocol ImageDownLoaderDelegate<NSObject>
// 2,声明两个方法
// 成功
- (void) imageDownSucceedWithData:(NSData *)data;
// 失败
- (void) imageDownFaildeWithData:(NSError *)error;
@end
@interface ImageDownLoader : NSObject
// 3,声明代理属性
@property (nonatomic ,assign)id<ImageDownLoaderDelegate> delegate;
// 声明一个初始化方法
- (id) initWithImageUrl:(NSString *)imageUrl delegate:(id<ImageDownLoaderDelegate>)delegate;
- (void)start;
- (void)cancel;
@end
#import "ImageDownLoader.h"
// 属性要想写.m里面 写一个延展
@interface ImageDownLoader ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
//声明一个可变的data接受收据 直接复制粘贴过来
@property (nonatomic, retain)NSMutableData *data;
// 链接声明 方便终止请求
@property (nonatomic, retain)NSURLConnection *connection;
@end
@implementation ImageDownLoader
- (void)dealloc
{
// 终止请求最好写在控制器里面
// [_connection cancel];
[_connection release];
[_data release];
[super dealloc];
}
// 实现声明的一个初始化方法
- (id) initWithImageUrl:(NSString *)imageUrl delegate:(id<ImageDownLoaderDelegate>)delegate
{
self = [super init];
if (self) {
// 设置代理
_delegate = delegate;
// 进行图片的请求
// 1,创建一个NSURL
NSURL *url = [NSURL URLWithString:imageUrl];
// 2, 用url对象创建一个请求
NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30.0];
[requst setHTTPMethod:@"GET"];
// 3, 用请求创建一个链接(使用代理方法)
self.connection = [NSURLConnection connectionWithRequest:requst delegate:self];
// 4,开启链接
// 最好用这个类的对象 可以控制开始 或者终止增加灵活性
// [self.connection start];
}
return self;
}
// 代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// 初始化data
self.data = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 拼接data
[self.data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// 加载完成 使用代理 把data传给控制器 (因为imageView在控制器创建)
// 外面设置了代理 并且实现了代理中的方法
// 才能让代理去干活
if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownSucceedWithData:)]) {
[_delegate imageDownSucceedWithData:self.data];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownFaildeWithData:)]) {
[_delegate imageDownFaildeWithData:error];
}
}
- (void)start
{
[self.connection start];
}
- (void)cancel
{
[self.connection cancel];
}
@end