Authored by Justin R. Miller

Merge branch 'develop' of github.com:mapbox/mapbox-ios-sdk into develop

... ... @@ -43,5 +43,6 @@
- (void)setPurgeStrategy:(RMCachePurgeStrategy)theStrategy;
- (void)setCapacity:(NSUInteger)theCapacity;
- (void)setMinimalPurge:(NSUInteger)thePurgeMinimum;
- (void)setExpiryPeriod:(NSTimeInterval)theExpiryPeriod;
@end
... ...
... ... @@ -57,6 +57,7 @@
RMCachePurgeStrategy purgeStrategy;
NSUInteger capacity;
NSUInteger minimalPurge;
NSTimeInterval expiryPeriod;
}
@synthesize databasePath;
... ... @@ -169,6 +170,13 @@
minimalPurge = theMinimalPurge;
}
- (void)setExpiryPeriod:(NSTimeInterval)theExpiryPeriod
{
expiryPeriod = theExpiryPeriod;
srand(time(NULL));
}
- (UIImage *)cachedImage:(RMTile)tile withCacheKey:(NSString *)aCacheKey
{
// RMLog(@"DB cache check for tile %d %d %d", tile.x, tile.y, tile.zoom);
... ... @@ -203,6 +211,28 @@
if (capacity != 0 && purgeStrategy == RMCachePurgeStrategyLRU)
[self touchTile:tile withKey:aCacheKey];
if (expiryPeriod > 0)
{
if (rand() % 100 == 0)
{
[writeQueueLock lock];
[queue inDatabase:^(FMDatabase *db)
{
BOOL result = [db executeUpdate:@"DELETE FROM ZCACHE WHERE last_used < ?", [NSDate dateWithTimeIntervalSinceNow:-expiryPeriod]];
if (result == NO)
RMLog(@"Error expiring cache");
[[db executeQuery:@"VACUUM"] close];
}];
[writeQueueLock unlock];
tileCount = [self countTiles];
}
}
// RMLog(@"DB cache hit tile %d %d %d (%@)", tile.x, tile.y, tile.zoom, [RMTileCache tileHash:tile]);
return cachedImage;
... ... @@ -217,7 +247,7 @@
{
NSUInteger tilesInDb = [self count];
if (capacity <= tilesInDb)
if (capacity <= tilesInDb && expiryPeriod == 0)
[self purgeTiles:MAX(minimalPurge, 1+tilesInDb-capacity)];
// RMLog(@"DB cache insert tile %d %d %d (%@)", tile.x, tile.y, tile.zoom, [RMTileCache tileHash:tile]);
... ...
... ... @@ -108,8 +108,6 @@
- (NSURL *)URLForTile:(RMTile)tile
{
// flip y value per OSM-style
//
NSInteger zoom = tile.zoom;
NSInteger x = tile.x;
NSInteger y = tile.y;
... ...
... ... @@ -87,7 +87,7 @@
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (zoom == (short)ceilf(mapView.zoom))
if (zoom == (short)ceilf(mapView.adjustedZoomForRetinaDisplay))
{
int x = floor(rect.origin.x / rect.size.width),
y = floor(fabs(rect.origin.y / rect.size.height));
... ... @@ -103,7 +103,7 @@
}
else // Probably due to renderInContext:
{
zoom = (short)ceilf(mapView.zoom);
zoom = (short)ceilf(mapView.adjustedZoomForRetinaDisplay);
CGFloat rectSize = bounds.size.width / powf(2.0, (float)zoom);
int x1 = floor(rect.origin.x / rectSize),
... ...
... ... @@ -115,7 +115,8 @@ typedef enum {
@property (nonatomic, readonly) float screenScale;
@property (nonatomic, assign) NSUInteger boundingMask;
@property (nonatomic, assign) BOOL adjustTilesForRetinaDisplay;
@property (nonatomic, assign) BOOL adjustTilesForRetinaDisplay;
@property (nonatomic, readonly) float adjustedZoomForRetinaDisplay; // takes adjustTilesForRetinaDisplay and screen scale into account
@property (nonatomic, assign) float zoom; /// zoom level is clamped to range (minZoom, maxZoom)
@property (nonatomic, assign) float minZoom;
... ...
... ... @@ -135,11 +135,7 @@
tiledLayerView = nil;
overlayView = nil;
screenScale = 1.0;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
{
screenScale = [[[UIScreen mainScreen] valueForKey:@"scale"] floatValue];
}
screenScale = [UIScreen mainScreen].scale;
boundingMask = RMMapMinWidthBound;
adjustTilesForRetinaDisplay = NO;
... ... @@ -1394,6 +1390,14 @@
[self setCenterCoordinate:self.centerCoordinate animated:NO];
}
- (float)adjustedZoomForRetinaDisplay
{
if (!self.adjustTilesForRetinaDisplay && screenScale > 1.0)
return [self zoom] + 1.0;
return [self zoom];
}
- (RMProjection *)projection
{
return [[projection retain] autorelease];
... ...
... ... @@ -77,8 +77,8 @@
@synchronized (cachedClusterAnnotation)
{
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
[cachedClusterEnclosedAnnotations release]; cachedClusterEnclosedAnnotations = nil;
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
}
@synchronized (annotations)
... ... @@ -167,8 +167,8 @@
@synchronized (cachedClusterAnnotation)
{
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
[cachedClusterEnclosedAnnotations release]; cachedClusterEnclosedAnnotations = nil;
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
}
if (RMProjectedRectIntersectsProjectedRect(quadTreeBounds, northWestBoundingBox))
... ... @@ -418,8 +418,8 @@
{
@synchronized (cachedClusterAnnotation)
{
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
[cachedClusterEnclosedAnnotations release]; cachedClusterEnclosedAnnotations = nil;
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
}
enclosedAnnotations = [NSArray arrayWithArray:annotationsToCheck];
... ... @@ -435,8 +435,8 @@
{
if (cachedClusterAnnotation && [enclosedAnnotations count] != [cachedClusterEnclosedAnnotations count])
{
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
[cachedClusterEnclosedAnnotations release]; cachedClusterEnclosedAnnotations = nil;
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
}
}
... ... @@ -555,8 +555,8 @@
@synchronized (cachedClusterAnnotation)
{
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
[cachedClusterEnclosedAnnotations release]; cachedClusterEnclosedAnnotations = nil;
[cachedClusterAnnotation release]; cachedClusterAnnotation = nil;
}
[cachedEnclosedAnnotations release]; cachedEnclosedAnnotations = nil;
... ...
... ... @@ -65,8 +65,11 @@ typedef enum {
// This one has its own variable because we want to propagate cache hits down in
// the cache hierarchy up to the memory cache
RMMemoryCache *memoryCache;
NSTimeInterval expiryPeriod;
}
- (id)initWithExpiryPeriod:(NSTimeInterval)period;
+ (NSNumber *)tileHash:(RMTile)tile;
// Add another cache to the chain
... ...
... ... @@ -41,13 +41,14 @@
@implementation RMTileCache
- (id)init
- (id)initWithExpiryPeriod:(NSTimeInterval)period
{
if (!(self = [super init]))
return nil;
caches = [[NSMutableArray alloc] init];
memoryCache = nil;
expiryPeriod = period;
id cacheCfg = [[RMConfiguration configuration] cacheConfiguration];
if (!cacheCfg)
... ... @@ -87,6 +88,14 @@
return self;
}
- (id)init
{
if (!(self = [self initWithExpiryPeriod:0]))
return nil;
return self;
}
- (void)dealloc
{
[memoryCache release]; memoryCache = nil;
... ... @@ -230,11 +239,16 @@
RMLog(@"minimalPurge must be at least one and at most the cache capacity");
}
}
NSNumber *expiryPeriodNumber = [cfg objectForKey:@"expiryPeriod"];
if (expiryPeriodNumber != nil)
expiryPeriod = [expiryPeriodNumber intValue];
RMDatabaseCache *dbCache = [[[RMDatabaseCache alloc] initUsingCacheDir:useCacheDir] autorelease];
[dbCache setCapacity:capacity];
[dbCache setPurgeStrategy:strategy];
[dbCache setMinimalPurge:minimalPurge];
[dbCache setExpiryPeriod:expiryPeriod];
return dbCache;
}
... ...
... ... @@ -15,32 +15,33 @@
- (void)viewDidLoad
{
NSLog(@"viewDidLoad");
NSLog(@"viewDidLoad");
[super viewDidLoad];
CLLocationCoordinate2D firstLocation;
firstLocation.latitude = 51.2795;
firstLocation.longitude = 1.082;
CLLocationCoordinate2D firstLocation;
firstLocation.latitude = 51.2795;
firstLocation.longitude = 1.082;
self.mapView = [[[RMMapView alloc] initWithFrame:CGRectMake(10, 20, 300, 340)] autorelease];
[mapView setBackgroundColor:[UIColor greenColor]];
[[self view] addSubview:mapView];
[[self view] sendSubviewToBack:mapView];
self.mapView = [[[RMMapView alloc] initWithFrame:CGRectMake(10, 20, 300, 340)] autorelease];
self.mapView.adjustTilesForRetinaDisplay = NO;
[self.mapView setBackgroundColor:[UIColor greenColor]];
[[self view] addSubview:mapView];
[[self view] sendSubviewToBack:mapView];
}
- (void)dealloc
{
[mapView removeFromSuperview];
self.mapView = nil;
[super dealloc];
self.mapView = nil;
[super dealloc];
}
- (IBAction)doTheTest:(id)sender
{
CLLocationCoordinate2D secondLocation;
secondLocation.latitude = -43.63;
secondLocation.longitude = 172.66;
[self.mapView setCenterCoordinate:secondLocation];
CLLocationCoordinate2D secondLocation;
secondLocation.latitude = -43.50;
secondLocation.longitude = 172.56;
[self.mapView setCenterCoordinate:secondLocation];
}
- (IBAction)takeSnapshot:(id)sender
... ...