如何从 CGPoints 以编程方式创建 PKDrawing?
How do I create a PKDrawing programmatically, from CGPoints?
我看过这个 WWDC session 及其示例项目:https://developer.apple.com/documentation/pencilkit/inspecting_modifying_and_constructing_pencilkit_drawings
然而,当我尝试在我的绘图上绘制 CGPoints 时 canvas,没有任何显示。
这是我的设置:
var points: [CGPoint] = []
(500...1000).forEach { x in
(500...1000).forEach { y in
points.append(CGPoint(x: x, y: y))
}
}
let strokePoints = points.map {
PKStrokePoint(location: [=10=], timeOffset: 0, size: CGSize(uniform: 1), opacity: 2, force: 1, azimuth: 1, altitude: 1)
}
let strokePath = PKStrokePath(controlPoints: strokePoints, creationDate: Date())
let stroke = PKStroke(ink: PKInk(.pen, color: .systemGreen), path: strokePath)
canvasView.drawing = PKDrawing(strokes: [ stroke ])
我认为问题出在我的笔触大小上。这将起作用:
PKStrokePoint(location: point, timeOffset: 0, size: CGSize(uniform: 3), opacity: 1, force: 0, azimuth: 0, altitude: 0)
注意至少为3,1为不可见,2为半透明。这似乎与屏幕的比例(我的 iPad Pro 的 UIScreen.main.scale = 2.0
)无关,所以我只是将其硬编码为 3,以表示单个像素。
为了达到这个结果,我用铅笔在屏幕上画了一个像素并记录了它的内容,它显示了这些参数:
▿ PencilKit.PKStrokePoint
- strokePoint: <PKStrokePoint: 0x600001ad8f60 location={445, 333.5} timeOffset=0.000000 size={3.084399938583374, 3.084399938583374} opacity=0.999985 azimuth=-3.141593 force=0.000000 altitude=1.570796> #0
- super: NSObject
然后我研究了这些值(大小、不透明度、方位角、力和高度),并认为 none 除了大小和不透明度很重要。这就是为什么我在我的代码中将它们全部设置为零值。
我看过这个 WWDC session 及其示例项目:https://developer.apple.com/documentation/pencilkit/inspecting_modifying_and_constructing_pencilkit_drawings
然而,当我尝试在我的绘图上绘制 CGPoints 时 canvas,没有任何显示。
这是我的设置:
var points: [CGPoint] = []
(500...1000).forEach { x in
(500...1000).forEach { y in
points.append(CGPoint(x: x, y: y))
}
}
let strokePoints = points.map {
PKStrokePoint(location: [=10=], timeOffset: 0, size: CGSize(uniform: 1), opacity: 2, force: 1, azimuth: 1, altitude: 1)
}
let strokePath = PKStrokePath(controlPoints: strokePoints, creationDate: Date())
let stroke = PKStroke(ink: PKInk(.pen, color: .systemGreen), path: strokePath)
canvasView.drawing = PKDrawing(strokes: [ stroke ])
我认为问题出在我的笔触大小上。这将起作用:
PKStrokePoint(location: point, timeOffset: 0, size: CGSize(uniform: 3), opacity: 1, force: 0, azimuth: 0, altitude: 0)
注意至少为3,1为不可见,2为半透明。这似乎与屏幕的比例(我的 iPad Pro 的 UIScreen.main.scale = 2.0
)无关,所以我只是将其硬编码为 3,以表示单个像素。
为了达到这个结果,我用铅笔在屏幕上画了一个像素并记录了它的内容,它显示了这些参数:
▿ PencilKit.PKStrokePoint
- strokePoint: <PKStrokePoint: 0x600001ad8f60 location={445, 333.5} timeOffset=0.000000 size={3.084399938583374, 3.084399938583374} opacity=0.999985 azimuth=-3.141593 force=0.000000 altitude=1.570796> #0
- super: NSObject
然后我研究了这些值(大小、不透明度、方位角、力和高度),并认为 none 除了大小和不透明度很重要。这就是为什么我在我的代码中将它们全部设置为零值。