Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift iOS 11没有';t从MKMapView抓取屏幕_Swift_Mapkit_Ios11_Swift4 - Fatal编程技术网

Swift iOS 11没有';t从MKMapView抓取屏幕

Swift iOS 11没有';t从MKMapView抓取屏幕,swift,mapkit,ios11,swift4,Swift,Mapkit,Ios11,Swift4,我有一个应用程序,可以显示用户在MKMapView上走过的位置。当用户离开地图视图时,应用程序会抓取屏幕并将图像保存在磁盘上。直到iOSS 10.3,这种方法始终是成功的。在iOS 11.0中,屏幕抓取是一个空白图像。我没有收到来自xcode的通知,说有一些更改,需要调整代码 有趣的是,从文本页面抓取的屏幕仍然被成功抓取和保存 有没有人遇到过同样的问题并得到了解决方案 到目前为止一直成功的代码是: override func viewWillDisappear(_ animated: Bool)

我有一个应用程序,可以显示用户在MKMapView上走过的位置。当用户离开地图视图时,应用程序会抓取屏幕并将图像保存在磁盘上。直到iOSS 10.3,这种方法始终是成功的。在iOS 11.0中,屏幕抓取是一个空白图像。我没有收到来自xcode的通知,说有一些更改,需要调整代码

有趣的是,从文本页面抓取的屏幕仍然被成功抓取和保存

有没有人遇到过同样的问题并得到了解决方案

到目前为止一直成功的代码是:

override func viewWillDisappear(_ animated: Bool) {

    //Set the full file name under which the track will be saved.
    let fileBaseName = self.imageName.appending(String(describing: (self.display?.trackDate)!))
    let fileFullName = fileBaseName.appending(".png")

    //Check if the image already has been saved
    if !AuxiliaryObjects.shared.getImageFileName(with: fileFullName ) {

        //Create the sizes of the capture
        let screenRect = self.trackMapView.frame
        let screenSize = screenRect.size
        let screenScale = UIScreen.main.scale
        var grabRect = self.trackMapView.convertRegion(self.mapRegion, toRectTo: self.view)
        var heightAdjustment : CGFloat = 0.0

        //Grab the image from the screen
        UIGraphicsBeginImageContextWithOptions(screenSize, false, screenScale)
        self.trackMapView.drawHierarchy(in: screenRect, afterScreenUpdates: true)
        let myImage = UIGraphicsGetImageFromCurrentImageContext()

        grabRect.origin.x *= (myImage?.scale)!
        grabRect.origin.y *= (myImage?.scale)!
        grabRect.size.width *= (myImage?.scale)!
        grabRect.size.height *= (myImage?.scale)!
        let grabImage = (myImage?.cgImage)!.cropping(to: grabRect)
        let mapImage = UIImage(cgImage: grabImage!)

        UIGraphicsEndImageContext()

        AuxiliaryObjects.shared.save(image: mapImage, with: fileFullName, and: self.imageName)
        self.display?.displayImage = AuxiliaryObjects.shared.getImage(with: fileFullName, with: self.tableImageRect)!
    } else {
        self.display?.displayImage = AuxiliaryObjects.shared.getImage(with: fileFullName, with: self.tableImageRect)!
    }
}

我在苹果公司提交了一份代码级支持请求,以获得问题的答案。苹果不支持在抓取地图套件屏幕时使用drawHierarhy。方法是使用MKMapSnapshotter实用程序创建一个MKMapSnapshot,然后通过将所有地图坐标转换为视图坐标来绘制线条和注释

由于这给了我一些获取镜像图像和正确转换坐标的问题,我决定使用层方法render(在:CGContext中),这为我提供了一个功能良好、非常高效的屏幕抓取

func creatSnapshot(with fileName: String) {

    UIGraphicsBeginImageContextWithOptions(self.trackMapView.frame.size, false, UIScreen.main.scale)
    let currentContext = UIGraphicsGetCurrentContext()
    self.trackMapView.layer.render(in: currentContext!)

    let contextImage = (UIGraphicsGetImageFromCurrentImageContext())!
    UIGraphicsEndImageContext()

    let region = self.trackMapView.region
    var cropRect = self.trackMapView.convertRegion(region, toRectTo: self.trackMapView.superview)

    cropRect.origin.x *= contextImage.scale
    cropRect.origin.y *= contextImage.scale
    cropRect.size.height *= contextImage.scale
    cropRect.size.width *= contextImage.scale

    let cgMapImage = contextImage.cgImage?.cropping(to: cropRect)
    let mapImage = UIImage(cgImage: cgMapImage!)

    AuxiliaryObjects.shared.save(image: mapImage, with: fileName, and: self.imageName)
    self.displayTrack?.displayImage = AuxiliaryObjects.shared.getImage(with: fileName, with: self.tableImageRect)!
    NotificationCenter.default.post(name: self.imageSavedNotification, object: self)
}