I have an MKMapView and am adding an overlay (MKOverlay) to it.
I would like the overlay to make the map view underneath to appear black & white (i.e. monochrome). Is there any way of doing this?
(I guess I could make the overlay translucent black/gray, but that's not the exact effect I would like.)
System details: developing an iOS 5 app in Xcode 4.2.
The most proper route, which sadly isn't yet available on iOS would be:
Your
MKOverlay
is at some point being used to create anMKOverlayView
, which will have aCALayer
as it descends fromUIView
. You can attachCIFilter
s ascompositingFilter
s toCALayer
s, which dictate how the view is composited with the background. You could attach a filter of typeCIColorMonochrome
to that.Because that option isn't available, you're going to have to leap through some major hurdles to do the work for yourself. You'll probably need to implement your own custom
MKOverlayView
that in itsdrawView
uses the techniques given in Apple's QA1703 to get the pixel contents of the map view in the relevant area (be careful not to end up in an infinite recursive loop though), transform those into black and white and present them as your view.Is that something it's worth investigating further or would you be happier to substitute the effect than to really delve into this stuff?
I needed same color manipulations on map, and I decided that making the whole map black and white may be better solution, here is how you can do this:
Here is some dirty code to do this:
Here you go - the map is in black and white colors. To do this dynamically to move monochrome map along with user's gesture you can subclass gesture recognizer, add it directly to map view. Then on every
touchesMoved
call you can call this redraw function while reusing context you once created. I tried this with apple map taking about 1/3 of the screen and it resulted in extremely low performance (about 3 FPS on iPhone 5s) and there are couple bugs you have to fix, such as: flying map on swipe gesture while monochrome image is static, frequency of map pins redrawing, redraw or animate every pop up that displayed in response to tap. You can try to render map in CALayer, not in Context, Apple recommends it to improve performance, but considering all the above I suppose if you want to see dynamic filtered elements it is better to use other more customizable map: GoogleMap or MapBox.