Skip to content

Instantly share code, notes, and snippets.

@M8Games
Last active August 29, 2015 14:19
Show Gist options
  • Save M8Games/274d0e255e915e0ff101 to your computer and use it in GitHub Desktop.
Save M8Games/274d0e255e915e0ff101 to your computer and use it in GitHub Desktop.
Swift - Scale up low resolution map tiles to show on retina screens
// 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