PageRenderTime 123ms CodeModel.GetById 97ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/MapView/Map/RMTileImage.m

http://github.com/route-me/route-me
Objective C | 240 lines | 162 code | 48 blank | 30 comment | 11 complexity | e322787360a3fa160680f3c01f51df4d MD5 | raw file
  1//
  2//  RMTileImage.m
  3//
  4// Copyright (c) 2008-2009, Route-Me Contributors
  5// All rights reserved.
  6//
  7// Redistribution and use in source and binary forms, with or without
  8// modification, are permitted provided that the following conditions are met:
  9//
 10// * Redistributions of source code must retain the above copyright notice, this
 11//   list of conditions and the following disclaimer.
 12// * Redistributions in binary form must reproduce the above copyright notice,
 13//   this list of conditions and the following disclaimer in the documentation
 14//   and/or other materials provided with the distribution.
 15//
 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26// POSSIBILITY OF SUCH DAMAGE.
 27#import "RMGlobalConstants.h"
 28#import "RMTileImage.h"
 29#import "RMWebTileImage.h"
 30#import "RMTileLoader.h"
 31#import "RMFileTileImage.h"
 32#import "RMDBTileImage.h"
 33#import "RMTileCache.h"
 34#import "RMPixel.h"
 35#import <QuartzCore/QuartzCore.h>
 36
 37@implementation RMTileImage
 38
 39@synthesize tile, layer, lastUsedTime;
 40
 41- (id) initWithTile: (RMTile)_tile
 42{
 43	if (![super init])
 44		return nil;
 45	
 46	tile = _tile;
 47	layer = nil;
 48	lastUsedTime = nil;
 49	screenLocation = CGRectZero;
 50
 51        [self makeLayer];
 52
 53	[self touch];
 54	
 55	[[NSNotificationCenter defaultCenter] addObserver:self
 56						selector:@selector(tileRemovedFromScreen:)
 57						name:RMMapImageRemovedFromScreenNotification object:self];
 58		
 59	return self;
 60}
 61	 
 62-(void) tileRemovedFromScreen: (NSNotification*) notification
 63{
 64	[self cancelLoading];
 65}
 66
 67-(id) init
 68{
 69	[NSException raise:@"Invalid initialiser" format:@"Use the designated initialiser for TileImage"];
 70	[self release];
 71	return nil;
 72}
 73
 74+ (RMTileImage*) dummyTile: (RMTile)tile
 75{
 76	return [[[RMTileImage alloc] initWithTile:tile] autorelease];
 77}
 78
 79- (void)dealloc
 80{
 81//	RMLog(@"Removing tile image %d %d %d", tile.x, tile.y, tile.zoom);
 82	
 83	[[NSNotificationCenter defaultCenter] removeObserver:self];
 84
 85	[layer release]; layer = nil;
 86	[lastUsedTime release]; lastUsedTime = nil;
 87	
 88	[super dealloc];
 89}
 90
 91-(void)draw
 92{
 93}
 94
 95+ (RMTileImage*)imageForTile:(RMTile) _tile withURL: (NSString*)url
 96{
 97	return [[[RMWebTileImage alloc] initWithTile:_tile FromURL:url] autorelease];
 98}
 99
100+ (RMTileImage*)imageForTile:(RMTile) _tile fromFile: (NSString*)filename
101{
102	return [[[RMFileTileImage alloc] initWithTile:_tile FromFile:filename] autorelease];
103}
104
105+ (RMTileImage*)imageForTile:(RMTile) tile withData: (NSData*)data
106{
107	UIImage *image = [[UIImage alloc] initWithData:data];
108	RMTileImage *tileImage;
109
110	if (!image)
111		return nil;
112
113	tileImage = [[self alloc] initWithTile:tile];
114	[tileImage updateImageUsingImage:image];
115	[image release];
116	return [tileImage autorelease];
117}
118
119+ (RMTileImage*)imageForTile:(RMTile) _tile fromDB: (FMDatabase*)db
120{
121	return [[[RMDBTileImage alloc] initWithTile: _tile fromDB:db] autorelease];
122}
123
124-(void) cancelLoading
125{
126	[[NSNotificationCenter defaultCenter] postNotificationName:RMMapImageLoadingCancelledNotification
127														object:self];
128}
129
130
131- (BOOL)updateImageUsingData: (NSData*) data
132{
133    UIImage *image = [UIImage imageWithData:data];
134    if ( !image ) {
135        return NO;
136    }
137       [self updateImageUsingImage:image];
138
139       NSDictionary *d = [NSDictionary dictionaryWithObject:data forKey:@"data"];
140       [[NSNotificationCenter defaultCenter] postNotificationName:RMMapImageLoadedNotification object:self userInfo:d];
141    
142    return YES;
143}
144
145- (void)updateImageUsingImage: (UIImage*) rawImage
146{
147	layer.contents = (id)[rawImage CGImage];
148//	[self animateIn];
149}
150
151- (BOOL)isLoaded
152{
153	return (layer != nil && layer.contents != NULL);
154}
155
156- (NSUInteger)hash
157{
158	return (NSUInteger)RMTileHash(tile);
159}
160
161-(void) touch
162{
163	[lastUsedTime release];
164	lastUsedTime = [[NSDate date] retain];
165}
166
167- (BOOL)isEqual:(id)anObject
168{
169	if (![anObject isKindOfClass:[RMTileImage class]])
170		return NO;
171
172	return RMTilesEqual(tile, [(RMTileImage*)anObject tile]);
173}
174
175- (void)makeLayer
176{
177	if (layer == nil)
178	{
179		layer = [[CALayer alloc] init];
180		layer.contents = nil;
181		layer.anchorPoint = CGPointZero;
182		layer.bounds = CGRectMake(0, 0, screenLocation.size.width, screenLocation.size.height);
183		layer.position = screenLocation.origin;
184		
185		NSMutableDictionary *customActions=[NSMutableDictionary dictionaryWithDictionary:[layer actions]];
186		
187		[customActions setObject:[NSNull null] forKey:@"position"];
188		[customActions setObject:[NSNull null] forKey:@"bounds"];
189		[customActions setObject:[NSNull null] forKey:kCAOnOrderOut];
190        [customActions setObject:[NSNull null] forKey:kCAOnOrderIn];
191
192		CATransition *fadein = [[CATransition alloc] init];
193		fadein.duration = 0.3;
194		fadein.type = kCATransitionReveal;
195		[customActions setObject:fadein forKey:@"contents"];
196		[fadein release];
197
198		
199		layer.actions=customActions;
200		
201		layer.edgeAntialiasingMask = 0;
202	}
203}
204
205- (void)moveBy: (CGSize) delta
206{
207	self.screenLocation = RMTranslateCGRectBy(screenLocation, delta);
208}
209
210- (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center
211{
212	self.screenLocation = RMScaleCGRectAboutPoint(screenLocation, zoomFactor, center);
213}
214
215- (CGRect) screenLocation
216{
217	return screenLocation;
218}
219
220- (void) setScreenLocation: (CGRect)newScreenLocation
221{
222//	RMLog(@"location moving from %f %f to %f %f", screenLocation.origin.x, screenLocation.origin.y, newScreenLocation.origin.x, newScreenLocation.origin.y);
223	screenLocation = newScreenLocation;
224	
225	if (layer != nil)
226	{
227		// layer.frame = screenLocation;
228		layer.position = screenLocation.origin;
229		layer.bounds = CGRectMake(0, 0, screenLocation.size.width, screenLocation.size.height);
230	}
231	
232	[self touch];
233}
234
235- (void) displayProxy:(UIImage*) img
236{
237        layer.contents = (id)[img CGImage]; 
238}
239
240@end