forked from xhan/PlutoLand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLImageRequest.m
144 lines (107 loc) · 3.25 KB
/
PLImageRequest.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// PLImageRequest.m
// PlutoLand
//
// Created by xu xhan on 7/15/10.
// Copyright 2010 xu han. All rights reserved.
//
#import "PLGlobal.h"
#import "PLImageRequest.h"
@implementation PLImageRequest
@synthesize url = _url;
@synthesize didFailSelector = _didFailSelector;
@synthesize didFinishSelector = _didFinishSelector;
@synthesize delegate = _delegate;
@synthesize response = _response;
@synthesize isCancelled , isFinished , isStarted;
@dynamic imageData;
static const int timeOutSec = 30;
#pragma mark -
#pragma mark NSObject
- (id)init
{
if (self = [super init]) {
_receivedData = [[NSMutableData alloc] init];
_delegate = nil;
isStarted = isFinished = isCancelled = NO;
_didFailSelector = @selector(imageRequestFailed:withError:);
_didFinishSelector = @selector(imageRequestSucceeded:);
}
return self;
}
- (id)initWithURL:(NSString*)urlStr
{
if (self = [self init]) {
[self requestGet:urlStr];
}
return self;
}
- (void)requestGet:(NSString*)urlStr
{
_url = [[NSURL URLWithString:urlStr] retain];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:_url];
[request setTimeoutInterval:timeOutSec];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[request release];
}
- (void)dealloc {
[_url release], _url = nil;
[_response release], _response = nil;
[_connection release], _connection =nil;
[super dealloc];
}
#pragma mark -
#pragma mark Public
- (void)start
{
[_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_connection start];
isStarted = YES;
}
- (void)cancel;
{
[_connection cancel];
isCancelled = YES;
}
- (NSData*)imageData
{
//!isFinished || remove check isFinished bcz after delegate (succeeded load)method the value
// will be set to true. and mostly we need get this data at the delegate method
// TODO: shall i add new property as taskFinished to work with HttpQueue ?
if ( [_receivedData length] == 0) {
return nil;
}
return _receivedData;
}
#pragma mark -
#pragma mark Delegate for NSURLRequest
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if ([self.delegate respondsToSelector:self.didFailSelector] ) {
[self.delegate performSelector:self.didFailSelector withObject:self withObject:error];
}
//PLLOG_STR(@"failed",nil);
self.isFinished = YES;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//PLLOG_STR(@"finished",nil);
// printf("image load finished\n");
if (statusCode != 200) {
NSError* error = [NSError errorWithDomain:@"PLImageFetchError" code:statusCode userInfo:nil];
if ([self.delegate respondsToSelector:self.didFailSelector] ) {
[self.delegate performSelector:self.didFailSelector withObject:self withObject:error];
}
}else {
if ([self.delegate respondsToSelector:self.didFinishSelector] ) {
[self.delegate performSelector:self.didFinishSelector withObject:self];
}
}
self.isFinished = YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aresponse {
self.response = (NSHTTPURLResponse*)aresponse;
statusCode = self.response.statusCode;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_receivedData appendData:data];
}
@end