如何更快地在 MKMapSnapshotter 上绘图?
How to make drawing on MKMapSnapshot faster?
我使用以下函数在 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{[=10=].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)
是一个非常昂贵的操作。如果您可以重构您的代码以仅调用一次,它将大大加快速度。
我使用以下函数在 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{[=10=].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)
是一个非常昂贵的操作。如果您可以重构您的代码以仅调用一次,它将大大加快速度。