错误消息在这里我是 swift 的新手。尝试调整 UIImage 高度和 UIImage 位置时在 .view 中出现错误。我只想从画布中获取绘图并对其进行自定义。
struct PKCanvas: UIViewRepresentable {
class Coordinator: NSObject, PKCanvasViewDelegate {
var pkCanvas: PKCanvas
init(_ pkCanvas: PKCanvas) {
self.pkCanvas = pkCanvas
}
}
@Binding var color:UIColor
@Binding var clear:Bool
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
let canvas = PKCanvasView()
public func makeUIView(context: Context) -> PKCanvasView {
let canvas = PKCanvasView()
canvas.tool = PKInkingTool(.pencil, color: color, width: 10)
canvas.backgroundColor = .clear
}
}
func preprocessImage() -> UIImage // {
var image = canvas.drawing.image(from: canvas.drawing.bounds, scale: 10.0)
if let newImage = UIImage(color: .black, size: CGSize(width: canvas.frame.width, height: view.frame.height)){
if let overlayedImage = newImage.image(byDrawingImage:
image,
inRect: CGRect(x: view.center.x, y: view.center.y, width:
view.frame.width, height: view.frame.height)){
image = overlayedImage
}
}
return image
}
我想在这里调用 preprocessImage
struct ContentView: View {
@State var color = UIColor.white
@State var clear = false
@State var classificationLabel: String = ""
let canvas = PKCanvasView()
var body: some View {
VStack{
PKCanvas(color: $color, clear:$clear)
VStack(){
Button("Pencil"){ self.color = UIColor.black }
Button("Classify"){
self.detectImage() //want to call here
}
Button("Clear Canvas"){ self.clear.toggle() }
}
}
}
}