【发布时间】:2020-07-24 00:33:07
【问题描述】:
我想在 iOS 上快速绘制一个二维数组。二维数组如heat map、depth map或segmentation map等。
在我的情况下,使用UIKit 框架,绘制大尺寸数组,如500x500 形状,太慢了。
// my solution, but it's too slow
override func draw(_ rect: CGRect) {
guard let ctx = UIGraphicsGetCurrentContext(),
let heatmap = self.heatmap else { return }
ctx.clear(rect);
let size = self.bounds.size
let heatmap_w = heatmap.count
let heatmap_h = heatmap.first?.count ?? 0
let w = size.width / CGFloat(heatmap_w)
let h = size.height / CGFloat(heatmap_h)
for j in 0..<heatmap_w {
for i in 0..<heatmap_h {
let value = heatmap[i][j]
let alpha: CGFloat = CGFloat(value)
guard alpha > 0 else { continue; }
let rect: CGRect = CGRect(x: CGFloat(i) * w, y: CGFloat(j) * h, width: w, height: h)
let color: UIColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: alpha*0.58)
let bpath: UIBezierPath = UIBezierPath(rect: rect)
color.set()
bpath.stroke()
bpath.fill()
}
}
} // end of draw(rect:)
我认为Metal 或CoreGraphics 框架与此问题有关,但我找不到合适的示例或材料。有什么推荐的方法吗?
热图示例
更新:
Here 是 MetalKit 的分段后处理实现示例。后处理的延迟从 iPhone 11 Pro 上的 240 ms 降至 1 ms。
我经常推荐MetalCamera。
新更新:
Here 是使用 MetalKit 和 Accelerate 框架的深度预测后处理实现示例。后处理的延迟从 iPhone 11 Pro 上的 15 ms 降至 1 ms。
【问题讨论】:
标签: ios core-graphics metal coreml accelerate-framework