/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. @implementation RMTileImage
  37. @synthesize tile, layer, lastUsedTime;
  38. - (id) initWithTile: (RMTile)_tile
  39. {
  40. if (![super init])
  41. return nil;
  42. tile = _tile;
  43. layer = nil;
  44. lastUsedTime = nil;
  45. screenLocation = CGRectZero;
  46. [self makeLayer];
  47. [self touch];
  48. [[NSNotificationCenter defaultCenter] addObserver:self
  49. selector:@selector(tileRemovedFromScreen:)
  50. name:RMMapImageRemovedFromScreenNotification object:self];
  51. return self;
  52. }
  53. -(void) tileRemovedFromScreen: (NSNotification*) notification
  54. {
  55. [self cancelLoading];
  56. }
  57. -(id) init
  58. {
  59. [NSException raise:@"Invalid initialiser" format:@"Use the designated initialiser for TileImage"];
  60. [self release];
  61. return nil;
  62. }
  63. + (RMTileImage*) dummyTile: (RMTile)tile
  64. {
  65. return [[[RMTileImage alloc] initWithTile:tile] autorelease];
  66. }
  67. - (void)dealloc
  68. {
  69. // RMLog(@"Removing tile image %d %d %d", tile.x, tile.y, tile.zoom);
  70. [[NSNotificationCenter defaultCenter] removeObserver:self];
  71. [layer release]; layer = nil;
  72. [lastUsedTime release]; lastUsedTime = nil;
  73. [super dealloc];
  74. }
  75. -(void)draw
  76. {
  77. }
  78. + (RMTileImage*)imageForTile:(RMTile) _tile withURL: (NSString*)url
  79. {
  80. return [[[RMWebTileImage alloc] initWithTile:_tile FromURL:url] autorelease];
  81. }
  82. + (RMTileImage*)imageForTile:(RMTile) _tile fromFile: (NSString*)filename
  83. {
  84. return [[[RMFileTileImage alloc] initWithTile:_tile FromFile:filename] autorelease];
  85. }
  86. + (RMTileImage*)imageForTile:(RMTile) tile withData: (NSData*)data
  87. {
  88. UIImage *image = [[UIImage alloc] initWithData:data];
  89. RMTileImage *tileImage;
  90. if (!image)
  91. return nil;
  92. tileImage = [[self alloc] initWithTile:tile];
  93. [tileImage updateImageUsingImage:image];
  94. [image release];
  95. return [tileImage autorelease];
  96. }
  97. + (RMTileImage*)imageForTile:(RMTile) _tile fromDB: (FMDatabase*)db
  98. {
  99. return [[[RMDBTileImage alloc] initWithTile: _tile fromDB:db] autorelease];
  100. }
  101. -(void) cancelLoading
  102. {
  103. [[NSNotificationCenter defaultCenter] postNotificationName:RMMapImageLoadingCancelledNotification
  104. object:self];
  105. }
  106. - (BOOL)updateImageUsingData: (NSData*) data
  107. {
  108. UIImage *image = [UIImage imageWithData:data];
  109. if ( !image ) {
  110. return NO;
  111. }
  112. [self updateImageUsingImage:image];
  113. NSDictionary *d = [NSDictionary dictionaryWithObject:data forKey:@"data"];
  114. [[NSNotificationCenter defaultCenter] postNotificationName:RMMapImageLoadedNotification object:self userInfo:d];
  115. return YES;
  116. }
  117. - (void)updateImageUsingImage: (UIImage*) rawImage
  118. {
  119. layer.contents = (id)[rawImage CGImage];
  120. // [self animateIn];
  121. }
  122. - (BOOL)isLoaded
  123. {
  124. return (layer != nil && layer.contents != NULL);
  125. }
  126. - (NSUInteger)hash
  127. {
  128. return (NSUInteger)RMTileHash(tile);
  129. }
  130. -(void) touch
  131. {
  132. [lastUsedTime release];
  133. lastUsedTime = [[NSDate date] retain];
  134. }
  135. - (BOOL)isEqual:(id)anObject
  136. {
  137. if (![anObject isKindOfClass:[RMTileImage class]])
  138. return NO;
  139. return RMTilesEqual(tile, [(RMTileImage*)anObject tile]);
  140. }
  141. - (void)makeLayer
  142. {
  143. if (layer == nil)
  144. {
  145. layer = [[CALayer alloc] init];
  146. layer.contents = nil;
  147. layer.anchorPoint = CGPointZero;
  148. layer.bounds = CGRectMake(0, 0, screenLocation.size.width, screenLocation.size.height);
  149. layer.position = screenLocation.origin;
  150. NSMutableDictionary *customActions=[NSMutableDictionary dictionaryWithDictionary:[layer actions]];
  151. [customActions setObject:[NSNull null] forKey:@"position"];
  152. [customActions setObject:[NSNull null] forKey:@"bounds"];
  153. [customActions setObject:[NSNull null] forKey:kCAOnOrderOut];
  154. [customActions setObject:[NSNull null] forKey:kCAOnOrderIn];
  155. CATransition *fadein = [[CATransition alloc] init];
  156. fadein.duration = 0.3;
  157. fadein.type = kCATransitionReveal;
  158. [customActions setObject:fadein forKey:@"contents"];
  159. [fadein release];
  160. layer.actions=customActions;
  161. layer.edgeAntialiasingMask = 0;
  162. }
  163. }
  164. - (void)moveBy: (CGSize) delta
  165. {
  166. self.screenLocation = RMTranslateCGRectBy(screenLocation, delta);
  167. }
  168. - (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center
  169. {
  170. self.screenLocation = RMScaleCGRectAboutPoint(screenLocation, zoomFactor, center);
  171. }
  172. - (CGRect) screenLocation
  173. {
  174. return screenLocation;
  175. }
  176. - (void) setScreenLocation: (CGRect)newScreenLocation
  177. {
  178. // RMLog(@"location moving from %f %f to %f %f", screenLocation.origin.x, screenLocation.origin.y, newScreenLocation.origin.x, newScreenLocation.origin.y);
  179. screenLocation = newScreenLocation;
  180. if (layer != nil)
  181. {
  182. // layer.frame = screenLocation;
  183. layer.position = screenLocation.origin;
  184. layer.bounds = CGRectMake(0, 0, screenLocation.size.width, screenLocation.size.height);
  185. }
  186. [self touch];
  187. }
  188. - (void) displayProxy:(UIImage*) img
  189. {
  190. layer.contents = (id)[img CGImage];
  191. }
  192. @end