MKAnnontation pin 图像调整大小问题
MKAnnontation pin image resize issue
MKAnnotation
的自定义图钉图片。当点击或缩放地图时,自定义图钉会被拉伸,并且家庭地图汽车图钉标记的开口尺寸很大。这是我在地图视图中得到的输出结果:
这是我目前试过的代码:
var pin = MKAnnotationView()
var userPinView: MKAnnotationView!
if annotation is MKUserLocation {
pin = mapView.view(for: annotation) ?? MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
let pinImage = UIImage(named: "carIcon3")
let size = CGSize(width: 38, height: 44)
UIGraphicsBeginImageContext(size)
pinImage!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
pin.image = resizedImag
userPinView = pin
userPinView.contentMode = .scaleAspectFill
userPinView.clipsToBounds = true
return pin
}
if !(annotation is MKPointAnnotation) {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
// annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
如何得到这样的结果:
已尝试使用当前位置标记。但是它崩溃了
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
UIView.animate(withDuration: 0.005) {
let angle = newHeading.trueHeading.toRadians() // convert from degrees to radians
self.userPinView.transform = CGAffineTransform(rotationAngle: CGFloat(angle)) // rotate the picture
}
}
这里是我为位置注释执行选择和取消选择的代码。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if annotView == true {
let heights = 70
let widths = 50
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {
view.frame.size = CGSize(width: widths, height: heights)
})
}
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
let heights = 70
let widths = 50
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {
view.frame.size = CGSize(width: view.frame.width - CGFloat(widths), height: view.frame.height - CGFloat(heights))
})
}
虽然我无法重现该问题,但建议进行一些更改:
分离视图配置逻辑,让子类MKAnnotationView
,像这样:
class CarAnnotationView: MKAnnotationView {
override var annotation: MKAnnotation? {
didSet {
let size = CGSize(width: 38, height: 44)
UIGraphicsBeginImageContext(size)
UIImage(named: "carIcon")?.draw(in: CGRect(origin: .zero, size: size))
self.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
}
在 mapView(_:viewFor:)
内进行更改以利用可重用视图,如下所示:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
userPinView = mapView.dequeueReusableAnnotationView(withIdentifier: "carId") as? CarAnnotationView
if (userPinView == nil) {
userPinView = CarAnnotationView(annotation: nil, reuseIdentifier: "carId")
}
userPinView.annotation = annotation
userPinView.setNeedsLayout()
// userPinView.centerOffset = CGPoint(x: 0, y: -view.bounds.midY)
return userPinView
}
guard annotation is MKPointAnnotation else { return nil }
let annotationIdentifier = "AnnotationIdentifier"
...
return annotationView
}
删除不需要的变量
var pin = MKAnnotationView()
MKAnnotation
的自定义图钉图片。当点击或缩放地图时,自定义图钉会被拉伸,并且家庭地图汽车图钉标记的开口尺寸很大。这是我在地图视图中得到的输出结果:
这是我目前试过的代码:
var pin = MKAnnotationView()
var userPinView: MKAnnotationView!
if annotation is MKUserLocation {
pin = mapView.view(for: annotation) ?? MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
let pinImage = UIImage(named: "carIcon3")
let size = CGSize(width: 38, height: 44)
UIGraphicsBeginImageContext(size)
pinImage!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
pin.image = resizedImag
userPinView = pin
userPinView.contentMode = .scaleAspectFill
userPinView.clipsToBounds = true
return pin
}
if !(annotation is MKPointAnnotation) {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
// annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
如何得到这样的结果:
已尝试使用当前位置标记。但是它崩溃了
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
UIView.animate(withDuration: 0.005) {
let angle = newHeading.trueHeading.toRadians() // convert from degrees to radians
self.userPinView.transform = CGAffineTransform(rotationAngle: CGFloat(angle)) // rotate the picture
}
}
这里是我为位置注释执行选择和取消选择的代码。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if annotView == true {
let heights = 70
let widths = 50
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {
view.frame.size = CGSize(width: widths, height: heights)
})
}
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
let heights = 70
let widths = 50
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {
view.frame.size = CGSize(width: view.frame.width - CGFloat(widths), height: view.frame.height - CGFloat(heights))
})
}
虽然我无法重现该问题,但建议进行一些更改:
分离视图配置逻辑,让子类
MKAnnotationView
,像这样:class CarAnnotationView: MKAnnotationView { override var annotation: MKAnnotation? { didSet { let size = CGSize(width: 38, height: 44) UIGraphicsBeginImageContext(size) UIImage(named: "carIcon")?.draw(in: CGRect(origin: .zero, size: size)) self.image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } } }
在
mapView(_:viewFor:)
内进行更改以利用可重用视图,如下所示:func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKUserLocation { userPinView = mapView.dequeueReusableAnnotationView(withIdentifier: "carId") as? CarAnnotationView if (userPinView == nil) { userPinView = CarAnnotationView(annotation: nil, reuseIdentifier: "carId") } userPinView.annotation = annotation userPinView.setNeedsLayout() // userPinView.centerOffset = CGPoint(x: 0, y: -view.bounds.midY) return userPinView } guard annotation is MKPointAnnotation else { return nil } let annotationIdentifier = "AnnotationIdentifier" ... return annotationView }
删除不需要的变量
var pin = MKAnnotationView()