I need to Draw over an image using PencilKit. the problem I face is I can't able to change the PencilKit tool picker tools (Eg pen to pencil). I am able to use only pen mode in the PKInkTool
Posted my code below
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
}
}
so this was my code.
anybody help to resolve the issue
I have updated the showPktoolkit method
@objc func showPKToolKit(){
if #available(iOS 14.0, *) {
toolPicker = PKToolPicker()
} else {
let window = parent?.view.window
toolPicker = PKToolPicker.shared(for: window!)
}
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
toolPicker.addObserver(self)
canvasView.becomeFirstResponder()
self.canvasView.drawingPolicy = .anyInput
}
Now it is working for me