gpt4 book ai didi

ios - 如何更快地在 MKMapSnapshotter 上绘图?

转载 作者:行者123 更新时间:2023-11-28 06:03:39 27 4
gpt4 key购买 nike

我使用以下函数在 MKMapView 的 UIImage 快照上绘制折线。该功能有效,但对我来说太慢了。有什么办法可以加快速度吗?

func drawLineOnImageFromLocations(image: UIImage, snapshot: MKMapSnapshot,
polylineLocations: [CLLocation], color: CGColor) -> UIImage {

UIGraphicsBeginImageContextWithOptions(image.size, true, 0)

// draw original image into the context
image.draw(at: CGPoint.zero)

// get the context for CoreGraphics
guard let context = UIGraphicsGetCurrentContext() else {
return image
}

context.beginPath()
context.setLineWidth(10)
context.setStrokeColor(color)
let coordinates = polylineLocations.map{$0.coordinate}
var points = [CGPoint]()
for coordinate in coordinates {
let point = snapshot.point(for: coordinate)
points.append(point)
}
context.addLines(between: points)
context.strokePath()

// get the image from the graphics context
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
// end the graphics context
UIGraphicsEndImageContext()

return resultImage!
}

最佳答案

结果调用:

UIGraphicsBeginImageContextWithOptions(image.size, true, 0)

是一个非常昂贵的操作。如果您可以重构您的代码以仅调用它一次,它将大大加快速度。

关于ios - 如何更快地在 MKMapSnapshotter 上绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48842337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com