Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting PKCanvasView to draw on image and save it

Situation

In my iOS app, I have a feature where I can take a picture and then, draw on it. I currently use an old Cocoapods library and I would like to switch to PencilKit.

Problem

Documentation and online tutorial are very unclear on the process/result. Is it supported by the framework to draw on an image ? Lots of SO post are saying to put the image in the background with a transparent PKCanvasView. that's seems a bit of a hack. Will it support cropping image ?

What I want

My goal is to achieve something similar as to what we have when we take a screenshot. An image that you can crop and draw on. Is the post-screenshot UI based on PencilKit? How can I achieve that ?

like image 849
Maxime Lapointe Avatar asked Nov 21 '25 20:11

Maxime Lapointe


1 Answers

You can put an image in the background and draw on by inserting an UIImageView as a subview to a PKCanvasView:

let canvasView = PKCanvasView()
canvasView.isOpaque = true
let imageView = UIImageView(image: UIImage(systemName: "pencil"))
canvasView.insertSubview(imageView, at: 1)
like image 180
Cihan Avatar answered Nov 24 '25 09:11

Cihan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!