RMWebTileImage.m
4.13 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// WebTileImage.m
// Images
//
// Created by Joseph Gentle on 1/09/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "RMWebTileImage.h"
#import "RMTileProxy.h"
#import <QuartzCore/CALayer.h>
#import "RMMapContents.h"
#import "RMTileLoader.h"
@implementation RMWebTileImage
- (id) initWithTile: (RMTile)_tile FromURL:(NSString*)urlStr
{
if (![super initWithTile:_tile])
return nil;
// NSLog(@"Loading image from URL %@ ...", urlStr);
NSURL *url = [NSURL URLWithString: urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSCachedURLResponse *cachedData = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
// proxy = [RMTileProxy loadingTile];
// [proxy retain];
// NSURLCache *cache = [NSURLCache sharedURLCache];
// NSLog(@"Cache mem size: %d / %d disk size: %d / %d", [cache currentMemoryUsage], [cache memoryCapacity], [cache currentDiskUsage], [cache diskCapacity]);
if (cachedData != nil)
{
// NSLog(@"Using cached image");
[self setImageToData:[cachedData data]];
}
else
{
BOOL startImmediately = [RMMapContents performExpensiveOperations];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:startImmediately];
if (connection == nil)
{
NSLog(@"Error: Connection is nil ?!?");
// proxy = [RMTileProxy errorTile];
// [proxy retain];
}
if (startImmediately == NO)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLoadingImage:) name:RMResumeExpensiveOperations object:nil];
}
}
// NSLog(@"... done. data size = %d", [imageData length]);
return self;
}
- (void) startLoadingImage: (NSNotification*)notification
{
if (connection != nil)
{
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[connection start];
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:RMResumeExpensiveOperations object:nil];
}
-(void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
// NSLog(@"Image dealloced");
// [proxy release];
// NSLog(@"loading cancelled because image dealloced");
[self cancelLoading];
[super dealloc];
}
-(void) cancelLoading
{
if (connection == nil)
return;
// NSLog(@"Image loading cancelled");
[connection cancel];
[connection release];
connection = nil;
[data release];
data = nil;
[super cancelLoading];
}
- (void)makeLayer
{
[super makeLayer];
// if (image == nil
// && layer != nil
// && layer.contents == nil)
// {
// layer.contents = (id)[[proxy image] CGImage];
// }
}
- (void)drawInRect:(CGRect)rect
{
if (image)
[super drawInRect:rect];
}
#pragma mark URL loading functions
// Delegate methods for loading the image
//– connection:didCancelAuthenticationChallenge: delegate method
//– connection:didReceiveAuthenticationChallenge: delegate method
//Connection Data and Responses
//– connection:willCacheResponse: delegate method
//– connection:didReceiveResponse: delegate method
//– connection:didReceiveData: delegate method
//– connection:willSendRequest:redirectResponse: delegate method
//Connection Completion
//– connection:didFailWithError: delegate method
//– connectionDidFinishLoading: delegate method
- (void)connection:(NSURLConnection *)_connection didReceiveResponse:(NSURLResponse *)response
{
if (data != nil)
[data release];
data = [[NSMutableData alloc] initWithCapacity:[response expectedContentLength]];
}
- (void)connection:(NSURLConnection *)_connection didReceiveData:(NSData *)newData
{
[data appendData:newData];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)_connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
return cachedResponse;
}
- (void)connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error
{
// proxy = [RMTileProxy errorTile];
[data release];
data = nil;
NSLog(@"Tile could not be loaded: %@", [error localizedDescription]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)_connection
{
[self setImageToData:data];
[data release];
data = nil;
[connection release];
connection = nil;
// NSLog(@"finished loading image");
}
@end