我有下面的函数,它需要三个参数来使用MKMapSnapshotter生成快照图像。该函数正确生成快照,但我想将生成的快照作为Byte数组返回。我认为可以通过使用回调,完成处理程序或类似的东西来执行,如果有任何答案,我很感激。
func generateImageSnapshot(location:CLLocationCoordinate2D, width: CGFloat, height: CGFloat) {
let options = MKMapSnapshotter.Options()
options.camera = MKMapCamera(lookingAtCenter: location, fromDistance: 300, pitch: 0, heading: 0)
options.mapType = .standard
options.size = CGSize(width: width, height: height)
let snapshotter = MKMapSnapshotter(options: options)
snapshotter.start() { snapshot, _ in
let mapImage = snapshot?.image
let finalImage = UIGraphicsImageRenderer(size: options.size).image { _ in
mapImage?.draw(at: .zero)
let pinView = MKPinAnnotationView(annotation: nil, reuseIdentifier: nil)
let pinImage = pinView.image
let point = snapshot?.point(for: location)
pinImage?.draw(at: point!)
}
return finalImage.pngData() // This line is needed to be returned by the function
}
}`
1条答案
按热度按时间fykwrbwg1#
我用下面的代码片段解决了这个问题: