我刚开始用Swift编程,我是一个完全的初学者。我想编写一个函数或类,通过接收坐标(如果MKLookAround.request中有一张照片),使用MKLookAround.Snapshotter保存所需位置的照片,但我不知道如何使用mapkit就绪类。我不想使用swiftUI,我只想保存来自不同地方的许多坐标的图片。在这里输入图像描述
发布于 2022-10-04 18:14:10
您可以创建一个MKLookAroundSceneRequest,获取它的scene,然后提供给MKLookAroundSnapshotter。
func snapshotImage(for coordinate: CLLocationCoordinate2D) async throws -> UIImage {
guard let scene = try await MKLookAroundSceneRequest(coordinate: coordinate).scene else {
throw LookaroundError.unableToCreateScene
}
let options = MKLookAroundSnapshotter.Options()
options.size = CGSize(width: 1000, height: 500)
return try await MKLookAroundSnapshotter(scene: scene, options: options).snapshot.image
}

它使用这个Error对象:
enum LookaroundError: Error {
case unableToCreateScene
}有关更多信息,请参见WWDC 2022 MapKit有什么新鲜事?或查看MKLookAroundSnapshotter在其样本工程中的使用。
如果要将其写入文件,请获取PNG (pngData)或JPG (jpgData)表示,并将其write到文件中:
let image = try await snapshotImage(for: coordinate)
let url = try FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appending(component: "test.png")
try image.pngData()?.write(to: url)或者,您可能希望向用户提供一个用户界面,用户可以通过UIActivityViewController指定他们想要对图像做什么。
let share = UIActivityViewController(activityItems: [image], applicationActivities: nil)
self.present(share, animated: true)https://stackoverflow.com/questions/73949844
复制相似问题