Authored by Justin R. Miller

allow for memory cache bypass

@@ -246,9 +246,11 @@ @@ -246,9 +246,11 @@
246 246
247 - (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey 247 - (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey
248 { 248 {
249 - // TODO: Converting the image here (again) is not so good...  
250 - NSData *data = UIImagePNGRepresentation(image); 249 + [self addDiskCachedImageData:UIImagePNGRepresentation(image) forTile:tile withCacheKey:aCacheKey];
  250 +}
251 251
  252 +- (void)addDiskCachedImageData:(NSData *)data forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey
  253 +{
252 if (_capacity != 0) 254 if (_capacity != 0)
253 { 255 {
254 NSUInteger tilesInDb = [self count]; 256 NSUInteger tilesInDb = [self count];
@@ -50,18 +50,31 @@ typedef enum : short { @@ -50,18 +50,31 @@ typedef enum : short {
50 * @return An image of the tile that can be used to draw a portion of the map. */ 50 * @return An image of the tile that can be used to draw a portion of the map. */
51 - (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)cacheKey; 51 - (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)cacheKey;
52 52
  53 +/** Returns an image from the cache if it exists.
  54 +* @param tile A desired RMTile.
  55 +* @param cacheKey The key representing a certain cache.
  56 +* @param shouldBypassMemoryCache Whether to only consult disk-based caches.
  57 +* @return An image of the tile that can be used to draw a portion of the map. */
  58 +- (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)cacheKey bypassingMemoryCache:(BOOL)shouldBypassMemoryCache;
  59 +
53 - (void)didReceiveMemoryWarning; 60 - (void)didReceiveMemoryWarning;
54 61
55 @optional 62 @optional
56 63
57 /** @name Adding to the Cache */ 64 /** @name Adding to the Cache */
58 65
59 -/** Adds a tile image to specified cache. 66 +/** Adds a tile image to the specified cache.
60 * @param image A tile image to be cached. 67 * @param image A tile image to be cached.
61 * @param tile The RMTile describing the map location of the image. 68 * @param tile The RMTile describing the map location of the image.
62 * @param cacheKey The key representing a certain cache. */ 69 * @param cacheKey The key representing a certain cache. */
63 - (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)cacheKey; 70 - (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)cacheKey;
64 71
  72 +/** Adds tile image data to the specified cache, bypassing the memory cache and only writing to disk. This is useful for instances where many tiles are downloaded directly to disk for later use offline.
  73 +* @param data The tile image data to be cached.
  74 +* @param tile The RMTile describing the map location of the image.
  75 +* @param cacheKey The key representing a certain cache. */
  76 +- (void)addDiskCachedImageData:(NSData *)data forTile:(RMTile)tile withCacheKey:(NSString *)cacheKey;
  77 +
65 /** @name Clearing the Cache */ 78 /** @name Clearing the Cache */
66 79
67 /** Removes all tile images from a cache. */ 80 /** Removes all tile images from a cache. */
@@ -164,10 +164,17 @@ @@ -164,10 +164,17 @@
164 return [NSNumber numberWithUnsignedLongLong:RMTileKey(tile)]; 164 return [NSNumber numberWithUnsignedLongLong:RMTileKey(tile)];
165 } 165 }
166 166
167 -// Returns the cached image if it exists. nil otherwise.  
168 - (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey 167 - (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey
169 { 168 {
170 - __block UIImage *image = [_memoryCache cachedImage:tile withCacheKey:aCacheKey]; 169 + return [self cachedImage:tile withCacheKey:aCacheKey bypassingMemoryCache:NO];
  170 +}
  171 +
  172 +- (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey bypassingMemoryCache:(BOOL)shouldBypassMemoryCache
  173 +{
  174 + __block UIImage *image = nil;
  175 +
  176 + if (!shouldBypassMemoryCache)
  177 + image = [_memoryCache cachedImage:tile withCacheKey:aCacheKey];
171 178
172 if (image) 179 if (image)
173 return image; 180 return image;
@@ -178,7 +185,7 @@ @@ -178,7 +185,7 @@
178 { 185 {
179 image = [cache cachedImage:tile withCacheKey:aCacheKey]; 186 image = [cache cachedImage:tile withCacheKey:aCacheKey];
180 187
181 - if (image != nil) 188 + if (image != nil && !shouldBypassMemoryCache)
182 { 189 {
183 [_memoryCache addImage:image forTile:tile withCacheKey:aCacheKey]; 190 [_memoryCache addImage:image forTile:tile withCacheKey:aCacheKey];
184 break; 191 break;
@@ -187,7 +194,7 @@ @@ -187,7 +194,7 @@
187 194
188 }); 195 });
189 196
190 - return image; 197 + return image;
191 } 198 }
192 199
193 - (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey 200 - (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey
@@ -208,6 +215,22 @@ @@ -208,6 +215,22 @@
208 }); 215 });
209 } 216 }
210 217
  218 +- (void)addDiskCachedImageData:(NSData *)data forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey
  219 +{
  220 + if (!data || !aCacheKey)
  221 + return;
  222 +
  223 + dispatch_sync(_tileCacheQueue, ^{
  224 +
  225 + for (id <RMTileCache> cache in _tileCaches)
  226 + {
  227 + if ([cache respondsToSelector:@selector(addDiskCachedImageData:forTile:withCacheKey:)])
  228 + [cache addDiskCachedImageData:data forTile:tile withCacheKey:aCacheKey];
  229 + }
  230 +
  231 + });
  232 +}
  233 +
211 - (void)didReceiveMemoryWarning 234 - (void)didReceiveMemoryWarning
212 { 235 {
213 LogMethod(); 236 LogMethod();