我需要使用PencilKit在图像上绘制。我面临的问题是我无法更改PencilKit工具选择器工具(例如,钢笔到铅笔)。我只能在PKInkTool中使用钢笔模式
发布我的代码如下
class ViewController: UIViewController, PKToolPickerObserver {
private var canvasView :PKCanvasView!
@IBOutlet weak var imgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
setup()
}
func setup() {
let navigationItem = navigationController?.navigationBar.topItem
let pencilButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
pencilButton.addTarget(self, action: #selector(showPKToolKit), for: .touchUpInside)
pencilButton.setImage(UIImage(systemName: "pencil.tip"), for: .normal)
pencilButton.tintColor = .tintColor
navigationItem?.rightBarButtonItem = UIBarButtonItem(customView: pencilButton)
let fileManager = FileManager.default
guard let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let urls: [URL]
do {
urls = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
} catch {
fatalError("Couldn't load files from documents directory")
}
do{
let data = try Data(contentsOf: urls[0])
print(data)
imgView.image = UIImage(data:data)
}catch{
print("failed to load image")
}
canvasView = PKCanvasView.init(frame: self.imgView.frame)
self.canvasView.isOpaque = false
self.view.addSubview(self.canvasView)
}
@objc func showPKToolKit(){
let toolPicker = PKToolPicker.init()
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
self.canvasView.drawingPolicy = .anyInput
}
}
这就是我的密码。
output video link
有人帮忙解决这个问题吗
1条答案
按热度按时间rqqzpn5f1#
我更新了showPktoolkit方法
现在它对我起作用了
video link