我正在使用 MKMapSnapshotter 获取特定位置的图像,但是 iOS 14 可以完美运行,但 iOS 15 却不行。
似乎 iOS 15 上的图像在某处的海洋中(蓝色背景)。不知道有没有其他人看到过这个问题?我使用了在这里找到的代码。还添加了一个 pin 以提供确切的位置,因此与链接的代码略有不同。任何指导或想法将不胜感激。
var body: some View {
GeometryReader { geometry in
Group {
if let image = snapshotImage {
Image(uiImage: image)
} else {
VStack {
Spacer()
HStack {
Spacer()
ProgressView().progressViewStyle(CircularProgressViewStyle())
Spacer()
}
Spacer()
}
.background(Color(UIColor.secondarySystemBackground))
}
}
.onAppear {
generateSnapshot(width: geometry.size.width, height: geometry.size.height)
}
}
}
func generateSnapshot(width: CGFloat, height: CGFloat) {
let rect = CGRect(x: 0, y: 0, width: width, height: height)
let region = MKCoordinateRegion(
center: self.location,
span: MKCoordinateSpan(
latitudeDelta: self.span,
longitudeDelta: self.span
)
)
// Map options.
let mapOptions = MKMapSnapshotter.Options()
mapOptions.region = region
mapOptions.size = CGSize(width: width, height: height)
mapOptions.showsBuildings = false
// Create the snapshotter and run it.
let snapshotter = MKMapSnapshotter(options: mapOptions)
snapshotter.start { (snapshotOrNil, errorOrNil) in
if let error = errorOrNil {
return
}
if let snapshot = snapshotOrNil {
let image = UIGraphicsImageRenderer(size: mapOptions.size).image { _ in
snapshot.image.draw(at: .zero)
let pinView = MKPinAnnotationView(annotation: nil, reuseIdentifier: nil)
let pinImage = pinView.image
var point = snapshot.point(for: location)
if rect.contains(point) {
point.x -= pinView.bounds.width / 2
point.y -= pinView.bounds.height / 2
point.x += pinView.centerOffset.x
point.y += pinView.centerOffset.y
pinImage?.draw(at: point)
}
}
DispatchQueue.main.async {
self.snapshotImage = image
}
}
}
}
}