Changing Color/Alpha/Filter to MKMapView iOS 6

2019-01-21 21:22发布

问题:

Is there a way to apply CI Filters to MKMapViews? Or something similar? I'm trying to not have my map based app looks so beige. Is there way to apply RGBA filters?

Any help/tutorials direction appreciated. I see nothing in the native documentation that talks about changing look for MKMapView.

回答1:

I don't think you can change the imagery before it is rendered to the screen. However, you can use an MKOverlayView over the entire world that achieves the same effect. The following should work, but treat it like pseudocode just to get you started.

@interface MapTileOverlay : NSObject <MKOverlay>
@end

@implementation MapTileOverlay
-(id)init {
    self = [super init];
    if(self) {
        boundingMapRect = MKMapRectWorld;
        coordinate = MKCoordinateForMapPoint(MKMapPointMake(boundingMapRect.origin.x + boundingMapRect.size.width/2, boundingMapRect.origin.y + boundingMapRect.size.height/2));
    }
    return self;
}
@end


@interface MapTileOverlayView : MKOverlayView
@end

@implementation MapTileOverlayView
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    CGContextSetBlendMode(context, kCGBlendModeMultiply);  //check docs for other blend modes
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.5);  //use whatever color to mute the beige
    CGContextFillRect(context, [self rectForMapRect:mapRect]);
}
@end

You need to have some class that implements the MKMapViewDelegate protocol to create the view...

@interface MapViewDelegate : NSObject<MKMapViewDelegate>
@end

@implementation MapViewDelegate
-(MKOverlayView*)mapView:(MKMapView*)mapView viewForOverlay:(id<MKOverlay>)overlay {
    if([overlay isKindOfClass:[MapTileOverlay class]]) {
        return [[MapTileOverlayView alloc] initWithOverlay:overlay];
    }
    return nil;
}

Finally, after you initialize your map, you need to set the delegate on the map and add the overlay...you must set the delegate before you add the overlay...

MapViewDelegate* delegate = [[MapViewDelegate alloc] init];  //you need to make this an iVar somewhere
[map setDelegate:delegate];
[map addOverlay:[[MapTileOverlay alloc] init]];


回答2:

You could add a view on top of the map view, nearly transparent with a slight color (e.g blueish to compensate that brown.)