2017-10-09 90 views
1

我有一個應用程序顯示用戶在MKMapView上走過的位置。當用戶離開地圖視圖時,應用程序抓住屏幕並將圖像保存在磁盤上。直到iOSS 10.3這種方法總是成功的。使用iOS 11.0時,屏幕抓圖是空白圖像。我沒有收到來自xcode的通知,說有一些變化,我需要調整代碼。iOS 11沒有從MKMapView抓取屏幕

有趣的是,文本頁面的抓取仍然抓取並保存成功。

有沒有人遇到同樣的問題,並得到了解決辦法?

的代碼一直是成功的,不外現在是:

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)! 
    } 
} 

回答

1

我提交了代碼級的支持請求在蘋果公司得到答案的問題。 Apple不支持使用drawHierarhy來抓取MapKit屏幕。要走的路是使用MKMapSnapshotter實用程序創建MKMapSnapshot,然後通過將所有地圖座標轉換爲視圖座標來繪製線條和註釋。

由於這給我一些獲得鏡像和正確翻譯座標的問題,我決定使用圖層方法渲染(在: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) 
}