【问题标题】:MKAnnontation pin image resize issueMKAnnontation pin 图像调整大小问题
【发布时间】:2019-01-08 03:46:12
【问题描述】:

MKAnnotation 的自定义 pin 图像。当点击或缩放地图时,自定义别针会被拉伸,并且主页地图汽车别针标记的开口尺寸很大。这是我在 mapview 中得到的输出结果:

这是我目前尝试过的代码:

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
    }
}

这里是我为位置注释执行 didselect 和取消选择的代码。

     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))

    })
}

【问题讨论】:

  • 在没有 UIGraphicsEndImageContext 的情况下尝试过,没有任何结果
  • 我是新手,你有没有推荐任何样品..
  • 就我个人而言,我会 (a) 将这辆车调整大小/绘图逻辑从那里拿出来,并只拥有适当大小的图像; (b) 我会为这两种类型的注解注册类; (c) 我会将注释视图的配置移动到这些子类中,调整它们设置各自的image 属性。例如。类似:gist.github.com/robertmryan/a33a94eb429b5a138553af409220f7eb。 MKMapViewDelegate 可能应该脱离配置注解视图的业务,而注解视图应该自行配置。
  • 不过,问题是我无法重现您的问题,所以我担心问题(如果不是缺少UIGraphicsEndImageContext 电话)与您在这里的问题无关。我建议您专注于创建simpler, reproducible example of the problem
  • 我的图片无法重现此问题,请提供原始图片,以便我进一步测试

标签: ios swift mkmapview mkannotationview


【解决方案1】:

虽然我无法重现该问题,但建议进行一些更改:

  1. 分离视图配置逻辑,让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()
            }
        }
    }
    
  2. 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
    
    }
    
  3. 删除不必要的变量

    var pin = MKAnnotationView()
    

【讨论】:

  • 我有两个问题 1.) 用户汽车图标不旋转 2.) 我将用户位置注释作为汽车图标并有多个其他位置标记,当在位置标记上选择时,用户位置汽车图标标记消失..
  • 我也更新了我的问题。你能帮我解决这个问题吗
  • 请发表单独的问题并将链接发给我,我们很乐意为您提供帮助!
  • 它还解决了我在长按或关闭并重新打开应用程序时 Pins 消失的问题
猜你喜欢
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多