-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageDownload.m
executable file
·73 lines (63 loc) · 1.62 KB
/
ImageDownload.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// ImageDownload.m
// Weibo_Test-0001
//
// Created by 1007 on 13-11-27.
// Copyright (c) 2013年 Ibokan. All rights reserved.
//
#import "ImageDownload.h"
@implementation ImageDownload
-(ImageDownload *)initWithURlString:(NSString *)urlStr
{
self=[super init];
if (self)
{
NSURL *url=[NSURL URLWithString:urlStr];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
}
return self;
}
//块方法
-(void)setBlock:(Block_fun)aBlock
{
if (block_data!=aBlock)
{
Block_release(block_data);
block_data=Block_copy(aBlock);
}
}
//响应请求
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
receiveData=[[NSMutableData alloc] initWithCapacity:0];
AllLength=[response expectedContentLength];
}
//下载数据时调用的方法
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receiveData appendData:data];
long long currentDataLength=[receiveData length];
if (block_data)
{
block_data(receiveData,((float)currentDataLength)/AllLength);
}
}
//下载完毕时调用
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//下载完毕要做的事
// [self.delegate sendReceiveData:receiveData];
}
//网络链接出错时调用
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"出错原因:%@", [error localizedDescription]);
}
- (void)dealloc
{
[receiveData release];
Block_release(block_data);
[super dealloc];
}
@end