Moj*_*ojo 4 mapkit mkmapview ios mapkitannotation
我正在为 iOS 构建一个应用程序,它允许用户基于位置存储图像。现在我想使用 iOS 中最新的AnnotationView( MKMarkerAnnotationView) 来实现此目的,因为它提供了自动集群。
我的问题是,这个类需要 aglyphTintColor如果没有提供它设置为UIColor().red. iOS 使用这种颜色来为整个图像着色。之后图像就只有这种颜色。我尝试将其设置glyphTintColor为,nil但这会导致图像变红。我也尝试将其设置为,UIColor(red:1.00, green:1.00, blue:1.00, alpha:0.0)但之后图像消失了,您只能看到标记本身。
我希望标记以原始颜色而不是单色显示图像。
谁能帮我解决这个问题吗?
这是我的完整代码:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let identifier = "PinAnnotationIdentifier"
var pinView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
pinView?.annotation = annotation
pinView?.clusteringIdentifier = "clusterId"
pinView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
pinView?.canShowCallout = false
pinView?.glyphImage = UIImage(named: "image")
return pinView
}
Run Code Online (Sandbox Code Playgroud)
你打电话时
pinView?.glyphImage = UIImage(named: "image")
Run Code Online (Sandbox Code Playgroud)
pinView来电
glyphImage?.withRenderingMode(.alwaysTemplate)
Run Code Online (Sandbox Code Playgroud)
这使得解决问题变得简单:只需覆盖withRenderingMode:
import UIKit
class OriginalUIImage: UIImage {
convenience init?(named name: String) {
guard let image = UIImage(named: name),
nil != image.cgImage else {
return nil
}
self.init(cgImage: image.cgImage!)
}
override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
// both return statements work:
return self
// return super.withRenderingMode(.alwaysOriginal)
}
}
Run Code Online (Sandbox Code Playgroud)
现在你可以使用
pinView?.glyphImage = OriginalUIImage(named: "image")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3801 次 |
| 最近记录: |