RMTileImage.m
6.7 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// Tile.m
// Images
//
// Created by Joseph Gentle on 13/08/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "RMTileImage.h"
#import "RMWebTileImage.h"
#import "RMTileLoader.h"
#import "RMFileTileImage.h"
#import "RMTileCache.h"
#import "RMPixel.h"
#import <QuartzCore/QuartzCore.h>
NSString * const RMMapImageLoadedNotification = @"MapImageLoaded";
NSString * const RMMapImageLoadingCancelledNotification = @"MapImageLoadingCancelled";
@implementation RMTileImage
@synthesize tile, layer, image, lastUsedTime;
- (id) initWithTile: (RMTile)_tile AddToCache: (BOOL) addToCache
{
if (![super init])
return nil;
tile = _tile;
image = nil;
layer = nil;
loadingPriorityCount = 0;
lastUsedTime = nil;
dataPending = nil;
screenLocation = CGRectMake(0, 0, 0, 0);
[self touch];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(tileRemovedFromScreen:)
name:RMMapImageRemovedFromScreenNotification object:self];
// Should this be done as a notification?
if (addToCache)
[[RMTileCache sharedCache] addTile:tile WithImage:self];
return self;
}
- (id) initWithTile: (RMTile)_tile
{
return [self initWithTile:_tile AddToCache: YES];
}
-(void) tileRemovedFromScreen: (NSNotification*) notification
{
[self cancelLoading];
}
-(id) init
{
[NSException raise:@"Invalid initialiser" format:@"Use the designated initialiser for TileImage"];
[self release];
return nil;
}
+ (RMTileImage*) dummyTile: (RMTile)tile
{
return [[[RMTileImage alloc] initWithTile:tile AddToCache:NO] autorelease];
}
- (void)dealloc
{
// NSLog(@"Removing tile image %d %d %d", tile.x, tile.y, tile.zoom);
[[NSNotificationCenter defaultCenter] removeObserver:self];
// if (image)
// CGImageRelease(image);
[image release];
[layer release];
[dataPending release];
[lastUsedTime release];
[super dealloc];
}
/*
- (id) increaseLoadingPriority
{
loadingPriorityCount++;
return self;
}
- (id) decreaseLoadingPriority
{
loadingPriorityCount--;
if (loadingPriorityCount == 0)
[self cancelLoading];
return self;
}*/
- (void)drawInRect:(CGRect)rect
{
[image drawInRect:rect];
/* if (image != NULL)
{
CGContextRef context = UIGraphicsGetCurrentContext();
NSLog(@"image width = %f", CGImageGetWidth(image));
// CGContextClipToRect(context, rect);
CGContextDrawImage(context, rect, image);
}*/
}
-(void)draw
{
[self drawInRect:screenLocation];
}
+ (RMTileImage*)imageWithTile: (RMTile) _tile FromURL: (NSString*)url
{
return [[[RMWebTileImage alloc] initWithTile:_tile FromURL:url] autorelease];
}
+ (RMTileImage*)imageWithTile: (RMTile) _tile FromFile: (NSString*)filename
{
return [[[RMFileTileImage alloc] initWithTile: _tile FromFile:filename] autorelease];
}
+ (RMTileImage*)imageWithTile: (RMTile) tile FromData: (NSData*)data
{
RMTileImage *image = [[RMTileImage alloc] initWithTile:tile];
[image setImageToData:data];
return [image autorelease];
}
-(void) cancelLoading
{
[[NSNotificationCenter defaultCenter] postNotificationName:RMMapImageLoadingCancelledNotification
object:self];
}
//
//- (void)setImageToData: (NSData*) data
//{
// image = [[UIImage imageWithData:data] retain];
//}
- (void) loadPendingData: (NSNotification*)notification
{
if (dataPending != nil)
{
[self setImageToData:dataPending];
[dataPending release];
dataPending = nil;
// NSLog(@"loadPendingData");
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:RMResumeExpensiveOperations object:nil];
}
- (void)setImageToData: (NSData*) data
{
if ([RMMapContents performExpensiveOperations] == NO)
{
// NSLog(@"storing data for later loading");
[data retain];
[dataPending release];
dataPending = data;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadPendingData:) name:RMResumeExpensiveOperations object:nil];
return;
}
// CGContextRef context =
CGDataProviderRef provider = CGDataProviderCreateWithCFData ((CFDataRef)data);
CGImageRef cgImage = CGImageCreateWithPNGDataProvider(provider, NULL, FALSE, kCGRenderingIntentDefault);
CGDataProviderRelease(provider);
// CGImageRetain(image);
// NSLog(@"setImageToData");
if (layer == nil)
{
image = [[UIImage imageWithCGImage:cgImage] retain];
}
else
{
// NSLog(@"Replacing layer contents with data");
layer.contents = (id)cgImage;
}
CGImageRelease(cgImage);
NSDictionary *d = [NSDictionary dictionaryWithObject:data forKey:@"data"];
[[NSNotificationCenter defaultCenter] postNotificationName:RMMapImageLoadedNotification
object:self
userInfo:d];
}
- (NSUInteger)hash
{
return (NSUInteger)RMTileHash(tile);
}
-(void) touch
{
[lastUsedTime release];
lastUsedTime = [NSDate init];
}
- (BOOL)isEqual:(id)anObject
{
if (![anObject isKindOfClass:[RMTileImage class]])
return NO;
return RMTilesEqual(tile, [(RMTileImage*)anObject tile]);
}
- (void)makeLayer
{
if (layer == nil)
{
layer = [[CALayer alloc] init];
layer.contents = nil;
layer.anchorPoint = CGPointMake(0.0f, 0.0f);
layer.bounds = CGRectMake(0, 0, screenLocation.size.width, screenLocation.size.height);
layer.position = screenLocation.origin;
NSMutableDictionary *customActions=[NSMutableDictionary dictionaryWithDictionary:[layer actions]];
[customActions setObject:[NSNull null] forKey:@"position"];
[customActions setObject:[NSNull null] forKey:@"bounds"];
[customActions setObject:[NSNull null] forKey:kCAOnOrderOut];
/* CATransition *fadein = [[CATransition alloc] init];
fadein.duration = 2.0;
fadein.type = kCATransitionFade;
[customActions setObject:fadein forKey:kCAOnOrderIn];
[fadein release];
*/
[customActions setObject:[NSNull null] forKey:kCAOnOrderIn];
layer.actions=customActions;
layer.edgeAntialiasingMask = 0;
// NSLog(@"location %f %f", screenLocation.origin.x, screenLocation.origin.y);
// NSLog(@"layer made");
}
if (image != nil)
{
layer.contents = (id)[image CGImage];
[image release];
image = nil;
// NSLog(@"layer contents set");
}
}
- (void)moveBy: (CGSize) delta
{
self.screenLocation = RMTranslateCGRectBy(screenLocation, delta);
}
- (void)zoomByFactor: (float) zoomFactor Near:(CGPoint) center
{
self.screenLocation = RMScaleCGRectAboutPoint(screenLocation, zoomFactor, center);
}
- (CGRect) screenLocation
{
return screenLocation;
}
- (void) setScreenLocation: (CGRect)newScreenLocation
{
// NSLog(@"location moving from %f %f to %f %f", screenLocation.origin.x, screenLocation.origin.y, newScreenLocation.origin.x, newScreenLocation.origin.y);
screenLocation = newScreenLocation;
if (layer != nil)
{
// layer.frame = screenLocation;
layer.position = screenLocation.origin;
layer.bounds = CGRectMake(0, 0, screenLocation.size.width, screenLocation.size.height);
}
[self touch];
}
@end