从地图拍摄的高分辨率屏幕

时间:2017-09-21 10:37:48

标签: ios swift mkmapview screenshot mkmapsnapshotter

我想要从我的Mapview的特定位置拍摄屏幕。我使用了以下代码。它的工作正常,但屏幕截图有点模糊。我想要高分辨率的屏幕截图。请检查我的代码。 谢谢!

func takeSnapShotForSaveSearch(_ centerPoint: CLLocationCoordinate2D) {

    var newRegion = mapHomes.region

    UIView.animate(withDuration: 0.0, animations: {() -> Void in

        let region = MKCoordinateRegionMakeWithDistance(centerPoint, 500.0, 500.0)
        //  region.center = centerPoint
        //  region.span.latitudeDelta = 1.0
        //  region.span.longitudeDelta = 1.0

        newRegion = region

    }, completion: {(_ finished: Bool) -> Void in

        var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let documentsDirectory = paths[0]

        let fileName = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("SaveSearch.png").absoluteString

        let options = MKMapSnapshotOptions()
        options.region = newRegion
        options.showsBuildings = false
        options.showsPointsOfInterest = false
        options.size = CGSize(width: 250.0, height: 250.0)

        let snapshotter = MKMapSnapshotter(options: options)
        snapshotter.start(completionHandler: { (snapShot, error) in

            let image = snapShot?.image

            if image != nil {
                let data = UIImagePNGRepresentation(image!)

                let fileURL = URL(string: fileName)

                do {
                    try data?.write(to: fileURL!)
                } catch {
                    print(error)
                }
            }
        })
    })
}

0 个答案:

没有答案