Authored by Justin R. Miller

allow for memory cache bypass

... ... @@ -246,9 +246,11 @@
- (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey
{
// TODO: Converting the image here (again) is not so good...
NSData *data = UIImagePNGRepresentation(image);
[self addDiskCachedImageData:UIImagePNGRepresentation(image) forTile:tile withCacheKey:aCacheKey];
}
- (void)addDiskCachedImageData:(NSData *)data forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey
{
if (_capacity != 0)
{
NSUInteger tilesInDb = [self count];
... ...
... ... @@ -50,18 +50,31 @@ typedef enum : short {
* @return An image of the tile that can be used to draw a portion of the map. */
- (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)cacheKey;
/** Returns an image from the cache if it exists.
* @param tile A desired RMTile.
* @param cacheKey The key representing a certain cache.
* @param shouldBypassMemoryCache Whether to only consult disk-based caches.
* @return An image of the tile that can be used to draw a portion of the map. */
- (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)cacheKey bypassingMemoryCache:(BOOL)shouldBypassMemoryCache;
- (void)didReceiveMemoryWarning;
@optional
/** @name Adding to the Cache */
/** Adds a tile image to specified cache.
/** Adds a tile image to the specified cache.
* @param image A tile image to be cached.
* @param tile The RMTile describing the map location of the image.
* @param cacheKey The key representing a certain cache. */
- (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)cacheKey;
/** 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.
* @param data The tile image data to be cached.
* @param tile The RMTile describing the map location of the image.
* @param cacheKey The key representing a certain cache. */
- (void)addDiskCachedImageData:(NSData *)data forTile:(RMTile)tile withCacheKey:(NSString *)cacheKey;
/** @name Clearing the Cache */
/** Removes all tile images from a cache. */
... ...
... ... @@ -164,10 +164,17 @@
return [NSNumber numberWithUnsignedLongLong:RMTileKey(tile)];
}
// Returns the cached image if it exists. nil otherwise.
- (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey
{
__block UIImage *image = [_memoryCache cachedImage:tile withCacheKey:aCacheKey];
return [self cachedImage:tile withCacheKey:aCacheKey bypassingMemoryCache:NO];
}
- (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey bypassingMemoryCache:(BOOL)shouldBypassMemoryCache
{
__block UIImage *image = nil;
if (!shouldBypassMemoryCache)
image = [_memoryCache cachedImage:tile withCacheKey:aCacheKey];
if (image)
return image;
... ... @@ -178,7 +185,7 @@
{
image = [cache cachedImage:tile withCacheKey:aCacheKey];
if (image != nil)
if (image != nil && !shouldBypassMemoryCache)
{
[_memoryCache addImage:image forTile:tile withCacheKey:aCacheKey];
break;
... ... @@ -187,7 +194,7 @@
});
return image;
return image;
}
- (void)addImage:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey
... ... @@ -208,6 +215,22 @@
});
}
- (void)addDiskCachedImageData:(NSData *)data forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey
{
if (!data || !aCacheKey)
return;
dispatch_sync(_tileCacheQueue, ^{
for (id <RMTileCache> cache in _tileCaches)
{
if ([cache respondsToSelector:@selector(addDiskCachedImageData:forTile:withCacheKey:)])
[cache addDiskCachedImageData:data forTile:tile withCacheKey:aCacheKey];
}
});
}
- (void)didReceiveMemoryWarning
{
LogMethod();
... ...