【发布时间】:2021-09-07 14:52:57
【问题描述】:
我想在 Apple Map 上使用网络图像作为注释。在我的应用程序的其他部分,我使用的是 SDWebImageSwiftUI,所以我也想在这里使用 SDWebImage。我知道 MapKit 有一个 SDWebImage 插件,但看起来我不能在显示之前对图像进行一些操作。我想要达到的效果和Facebook Messenger app很接近:
我将用户的方形图像存储在 DigitalOcean Space(兼容 Amazon S3 的存储桶)中,因此必须从服务器加载。获取图像后,我需要将其变为圆形并添加白色背景(就像信使应用程序一样)。
我这样创建注释视图:
class MapUserAnnotationView: MKAnnotationView {
static let reuseId = "quickEventUser"
var photo: String?
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
if let ann = annotation as? MapQuickEventUserAnnotation {
self.photo = ann.photo
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForDisplay() {
super.prepareForDisplay()
image = "???"
// Here I need to load image and make edit (probably)
}
}
我尝试过这样的事情:
override func prepareForDisplay() {
super.prepareForDisplay()
let img = UIImageView()
img.sd_setImage(with: URL(string: photo ?? ""), completed: {_,_,_,_ in })
image = img.image
}
但它不起作用(没有显示注释,没有调用完成并且没有图像加载错误日志)
我知道我遗漏了一些琐碎的东西,但我对 MapKit 的经验很少,我找不到任何关于实现类似东西的资料。
我应该如何实现它?我的思维方式是否接近正确的解决方案?
【问题讨论】:
-
你真的在 SwiftUI 应用中使用它吗?
-
是的,我使用
UIViewRepresentable来显示MKMapView。
标签: swift swiftui mapkit sdwebimage mapkitannotation