Last active
August 29, 2015 14:19
-
-
Save M8Games/274d0e255e915e0ff101 to your computer and use it in GitHub Desktop.
Swift - Scale up low resolution map tiles to show on retina screens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Somewhere in ViewController: | |
func changeMapOverlay() { | |
mapOverlay = MyTileOverlay( URLTemplate: "http://tile.openstreetmap.org/{z}/{x}/{y}.png" ) | |
mapOverlay!.canReplaceMapContent = true | |
mapOverlay!.tileSize = CGSize( width: 512, height: 512 ) | |
self.mapView.addOverlay( self.mapOverlay, level: .AboveLabels ) | |
} | |
class MyTileOverlay: MKTileOverlay { | |
override func loadTileAtPath(path: MKTileOverlayPath, result: ((NSData!, NSError!) -> Void)!) { | |
super.loadTileAtPath( path ) { ( data, error ) in | |
let tileImage = UIImage( data: data ) | |
let scale: CGFloat = 1.0 | |
let sizeChange = CGSize( width: 512, height: 512 ) | |
UIGraphicsBeginImageContextWithOptions( sizeChange, false, scale ) | |
tileImage!.drawInRect( CGRect(origin: CGPointZero, size: sizeChange) ) | |
let scaledImage = UIGraphicsGetImageFromCurrentImageContext() | |
result( UIImagePNGRepresentation( scaledImage ), error ) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment