PencilKit

RSS for tag

Capture touch input as an opaque drawing and turn it into high-quality images that can be displayed on iOS and macOS using PencilKit.

Posts under PencilKit tag

137 Posts

Post

Replies

Boosts

Views

Activity

Apple Pencil Pro Squeeze API
I wonder if an Apple engineer could confirm: will the Apple Pencil Pro squeeze functionality be detectable in the current API, or will this be a future iPadOS extension to gesture recognizers / UIKit? I’d like to start playing with the functionality if it’s detected behind an existing event though. (Long press?)
1
0
1.3k
May ’24
PencilKit's PKToolPicker shows normally on iOS, but not on Mac Catalyst.
DemoCode: import SwiftUI import UIKit import PencilKit class PencilKitViewController: UIViewController, PKCanvasViewDelegate, PKToolPickerObserver {       lazy var canvasView: PKCanvasView = {     let canvasView = PKCanvasView()      canvasView.drawingPolicy = .anyInput      canvasView.translatesAutoresizingMaskIntoConstraints = false      return canvasView    }()       lazy var toolPicker: PKToolPicker = {     let toolPicker = PKToolPicker()     toolPicker.showsDrawingPolicyControls = true     toolPicker.addObserver(self)     return toolPicker   }()       let drawing = PKDrawing()       override func viewDidLoad() {     super.viewDidLoad()     canvasView.drawing = drawing     canvasView.delegate = self     view.addSubview(canvasView)   }       override func viewDidLayoutSubviews() {     super.viewDidLayoutSubviews()     canvasView.frame = view.bounds   }       override func viewDidAppear(_ animated: Bool) {     super.viewDidAppear(animated)     toolPicker.setVisible(true, forFirstResponder: canvasView)     toolPicker.addObserver(canvasView)     canvasView.becomeFirstResponder()   }       // canvas   func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) {     print("drawing")   }       func canvasViewDidFinishRendering(_ canvasView: PKCanvasView) {         }       func canvasViewDidEndUsingTool(_ canvasView: PKCanvasView) {         }       func canvasViewDidBeginUsingTool(_ canvasView: PKCanvasView) {         } } // UIRepresentable for SwiftUI struct PencilKitView: UIViewControllerRepresentable {       class Coordinator {     var parentObserver: NSKeyValueObservation?   }       var onSubmit: ((UIImage?, Error?) -> Void)? = .none       func makeUIViewController(context: Context) -> PencilKitViewController {     let pencilKitViewController = PencilKitViewController()     context.coordinator.parentObserver = pencilKitViewController.observe(\.parent, changeHandler: { vc, _ in      })     return pencilKitViewController   }       func updateUIViewController(_ uiViewController: PencilKitViewController, context: Context) {   }       func makeCoordinator() -> Self.Coordinator { Coordinator() } } struct ContentView: View {       var onSubmit: ((UIImage?, Error?) -> Void)? = .none       var body: some View {       PencilKitView()   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } } iOS: macCatalyst:
3
0
2.2k
Apr ’24
When PKCanvasView is first drawn, existing drawing objects disappear.
When PKCanvasView is first drawn, existing drawing objects disappear. PKDrawing, which previously had drawings saved with PKCanvasView, was saved as separate data or file. After that, while creating a new PKCanvasView, I loaded the saved PKDrawing and reflected it in the new PKCanvasView. Below is the code. canvasView!.drawing = draw! Previously saved text or lines will be displayed normally. However, when I draw with the pen for the first time (when I touch the screen with the pen), the old writing or lines disappear. And after I leave a line or text, if I pan across the screen, the old text or line appears again. If this phenomenon occurs and you touch the screen again with the fan, this phenomenon will no longer occur. This phenomenon occurs the first time when a new PKCanvasView is declared and the previously saved PKDrawing is reflected. Could you please help me with why this phenomenon occurs and how to improve it?
4
0
994
Apr ’24
Blurry and low resolution of PKCanvasView, as overlayview from PDFView.
Hi, I am trying to make a simple note taking app that users can draw something on pdfview with apple pencil. (I used PDFKit, PencilKit for the code.) I followed the instruction code of WWDC22's "What's new in PDFKit." - "overlayProvider" (so you can see the code at the video.) I was able to draw something each view of pdf page. But the issue was, the resolution of overlayview or subview of pdfview is low. As far as I know, the pkcanvasview draws vertor-based drawings. So I never thought the image or the lines I draw will be that blurry. Is this buggy or is this the normal thing? (+ I added a uibutton as subview of pdfview and the button also looks blurry.) I even tried to scale up the all the subviews when the subviews' layout is done, using contentScaleFactor. PKCanvasView inherits UIScrollView, so I enlarged the frame of pkcanvas view and fixed the scale to below 1.0. If the pkcanvasview looks blurry and that is because somewhat zoomed in wrong way, zooming out should be the solution. But, didn't work. Still blurry. and any other stuff like changing frame or size. So, anyone having same problem with me, or anyone can give me any solution. Please help me. I wish this is bug thing that can be fixed in any moment. -> This image is little bit zoomed in. but the drawing is blurry. and this is the normal pkcanvasview drawing, just subview of view(of VC).
3
1
1.4k
Mar ’24
PencilKit does not draw on PKCanvasView on VisionOS1.1 RC
Hi, I have an app using PencilKit that works on VisionOS 1.0. It means, a user can pick an inking tool from PKToolPicker and draw on PKCanvasView. The app is now on available on Vision Pro AppStore as well. However, when I test the app on VisionOS 1.1 RC Simulator, I can pick an inking tool but when I try to draw on the canvas, it just scrolls and no drawing appears on the PKCanvasView. I also noticed that the VisionOS 1.0 Simulator has the FreeForm app where you can draw with PencilKit but the VisionOS 1.1 RC Simulator does not have the FreeForm app. Is this a known issue? Will it be fixed before the release or is there a change in API so I can update the app accordingly? Thanks Zafer
1
0
911
Mar ’24
PKDrawing.image crashes on iOS 17 (EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000210)
In 2 days we have observed in Crashlytics over 50 crashes related to PKDrawing.image with EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000210 So far all on iPads with A10, A12 and A13; 100% on iOS 17 (17.1.1, 17.2, 17.3) (while in others the percent of iOS 17 was around 60-80%). Context: Images of the varying frame and scale resulting in screen resolution are being generated sequentially (in a background serial queue called almost one after another when requested), updating one CALayer.contents (and only after this update on Main Thread the next generation is allowed). One zoomable PKCanvasView is present on screen. The crashing line in code: let image = drawing.image(from: frame, scale: renderScale).cgImage The questions: Is there anything that can be done apart from throttling generation? Can the circumstances of the crash be determined – are there any indications accessible in code before calling PKDrawing.image that app might crash? The traces: 0 AGXMetalA12 AGX::BlitContext<AGX::G11::Encoders, AGX::G11::Classes, AGX::G11::ObjClasses>::copyTextureToBuffer(IOGPUMetalResource const*, unsigned long, unsigned long, unsigned long, AGXA12FamilyTexture*, unsigned int, unsigned int, MTLOrigin, MTLSize, unsigned long) + 96 9 PencilKit PKDrawing.image(from:scale:) + 28 0 AGXMetalA13 <redacted> + 96 9 PencilKit PKDrawing.image(from:scale:) + 28 0 AGXMetalA10 <redacted> + 72 9 PencilKit $s9PencilKit9PKDrawingV5image4from5scaleSo7UIImageCSo6CGRectV_12CoreGraphics7CGFloatVtF + 24
2
1
1.2k
Feb ’24
How do I make UIImage conform to Transferable?
Hello! I've been teaching myself Swift and wanted to challenge myself by creating a drawing app. I want to add a feature that allows the user to share the drawing on their canvas, but I'm having some difficulty. I tried using a ShareLink, but it's asking that I conform in to Transferable. How do I do that? import SwiftUI import PencilKit struct ContentView: View { @State private var canvasView = PKCanvasView() var body: some View { Text("Let's draw!") GeometryReader { geometry in VStack { Spacer() HStack { Spacer() PencilKitView() .frame(width: geometry.size.width * 1, height: geometry.size.height * 0.7) Spacer() } Spacer() } } //share button Button("share with a friend") { let portionRect = CGRect(x: 0, y: 0, width: 100, height: 100) let scale: CGFloat = 1.0 let image = canvasView.drawing.image(from: portionRect, scale: scale) ShareLink( item: image, preview: SharePreview( "Share Preview", image: image ) ) } } } struct PencilKitView: UIViewRepresentable { typealias UIViewType = PKCanvasView let toolPicker = PKToolPicker() func makeUIView(context: Context) -> PKCanvasView { let pencilKitCanvasView = PKCanvasView() pencilKitCanvasView.drawingPolicy = PKCanvasViewDrawingPolicy.anyInput toolPicker.addObserver(pencilKitCanvasView) toolPicker.setVisible(true, forFirstResponder: pencilKitCanvasView) pencilKitCanvasView.becomeFirstResponder() return pencilKitCanvasView } func updateUIView(_ uiView: PKCanvasView, context: Context) { } } #Preview { ContentView() }
1
0
2.4k
Feb ’24
wwdc2022-10089 and various issues
Hi, I encounter various problems with inserting PKDrawing into a PDFAnnotation : First : After "page.addAnnotation(myCustomAnnotation)", saved document seems corrupted (affected pages are displayed with a "X" watermark covering the whole page), Second : The only way to extract PKDrawing from the annotation is unarchiveTopLevelObjectWithData: which is deprecated, Final : I'm not able to re-read PKDrawings to restore PKCanvasView undoManager. Does anyone have an idea on a correct way to do this? Thank you in advance and happy new year everyone!
0
0
709
Jan ’24
PKCanvasView: zoom+rotate gesture=ded performance
I'm trying to add rotation functionality to the PKCanvasView, so that rotation gesture rotates the drawing, I want it to be working together with zoom gesture too, which is already implemented in the PKCanvasView. Rotation works reasonably fine when rotation and zoom can't be performed simultaneously. The moment I enable simultaneous zoom+rotation performance of rotation gets noticably bad. Visually it looks like rotation only happens on noticable angles like 10,20,30.. degrees which looks choppy. Zoom at the same time looks smooth as before. I'm trying to understand why and how to fix this. The rotation gesture handler is here: @objc func handleRotation(_ gesture: UIRotationGestureRecognizer) { guard let curView = gesture.view as? PKCanvasView else {return} if gesture.state == .began || gesture.state == .changed { let rotation = gesture.rotation let gestureCenter = gesture.location(in: curView) let center = CGPoint(x: gestureCenter.x / curView.zoomScale, y: gestureCenter.y / curView.zoomScale) let finalTransform = CGAffineTransform(translationX: -center.x, y: -center.y) .concatenating(CGAffineTransform(rotationAngle: rotation)) .concatenating(CGAffineTransform(translationX: center.x, y: center.y)) curView.drawing.transform(using: finalTransform) gesture.rotation = 0 } } which just constructs rotation matrix around rotation gesture center and calls curView.drawing.transform(using: finalTransform). Rotation matrix is correct I think because without simultaneous zoom+rotation it rotates everything as intended and smoothly under any zoom. (Just for clarity: in the rotation handler the matrix does only the rotation, zoom is done in pinch gesture handler implemented by the PKCanvasView. The zoomScale is used in estimations only to figure out the content coodrinate). The handleRotation method is set as let rotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(handleRotation)) rotationGestureRecognizer.delegate = self canvasView.addGestureRecognizer(rotationGestureRecognizer) The simultaneous handling of zoom+rotation I enable with this code: func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } What I've tried: Tried to nable simultaneous recognition only for zoom+rotation (without the pan gesture and possible others), the effect is the same. For example rotation+pan work smoothly together. I've verified that the gesture handler is called very often, every 8-16 milliseconds (rotation matrix creation and curView.drawing.transform(using: finalTransform) is included in this time period) which should be enough for visually smooth rotation. Other observations: When zoom and rotation both are enabled the rotation sometimes gets completely stuck, zoom and pinch is responsive but rotation is not happening visually. Only when I release fingers from the screen the rotation unstucks and content is rotated as if it didn't stuck at all. During the "stuck" period the rotation handler is called with the same frequency it's just not being visually reflected. Other thoughts: It kind of looks like the tranformation I set in the rotation handler gets overwritten by transformation from the zoom handler. But pan+rotation works fine together which confuses this theory.
1
0
889
Dec ’23
SwiftUI Third Party Keyboard
Hello. I am developing an app that helps people with language issues communicate and for that I thought I could do a custom keyboard for them. I was wondering if there is a way of doing a 3rd party keyboard using SwiftUI (little to no UIKit) and if so, where can I find documentation/tutorials. I have looked with no success. Additionally, can we use a PKCanvasView inside of keyboards (ie, is there any restriction/policy)? I intend to use it so that users can draw the words they're looking for (I already have the model and know how to come up with the suggestions and everything) Thanks a lot!
0
0
798
Nov ’23
PencilKit: zoom PKCanvasView, drawing blurred (swift, iOS 15)
It seems PKCanvasView overrides the property UIScrollViewDelegate which inherits from the UIScrollView to PKCanvasViewDelegate. And does not provide access to UIScrollViewDelegate. In order to implement zooming, so I added a PKCanvasView into my own UIScrollView. And implemented delegate method viewForZooming in which return PKCanvasView. But all drawing in PKCanvasView was blurred when zooming or scale. How to re-render drawing after zoom to make it has reasonable stroke width and clear ? Some related code: let canvasView = PKCanvasView() let scrollView = UIScrollView() override func viewDidLoad() {     super.viewDidLoad()     self.view.addSubview(scrollView)     scrollView.addSubview(canvasView)     scrollView.delegate = self     scrollView.minimumZoomScale = 0.5     scrollView.maximumZoomScale = 2.5 } func viewForZooming(in scrollView: UIScrollView) -> UIView? {     return canvasView } Some solutions I had tried: 1: Reset PKCanvasView contentScaleFactor func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {     if let canvas = view {       let contentScale = scale * UIScreen.main.scale       canvas.contentScaleFactor = contentScale     }   } Not worked! 2: Re-render PKStroke: func reRender(_ scale: CGFloat) {     let newStrokeWidth = strokeWidth * scale     var newDrawingStrokes: [PKStroke] = []     for stroke in canvasView.drawing.strokes {       canvasView.tool = PKInkingTool(.pen, color: .red, width: newStrokeWidth)       var newPoints = [PKStrokePoint]()       stroke.path.forEach { (point) in         let newPoint = PKStrokePoint(location: point.location,                        timeOffset: point.timeOffset,                        size: CGSize(width: newStrokeWidth, height: newStrokeWidth),                        opacity: CGFloat(1), force: point.force,                        azimuth: point.azimuth, altitude: point.altitude)         newPoints.append(newPoint)       }       let newPath = PKStrokePath(controlPoints: newPoints, creationDate: Date())       let newStroke = PKStroke(ink: PKInk(.pen, color: UIColor.red), path: newPath)       newDrawingStrokes.append(newStroke)     }     let newDrawing = PKDrawing(strokes: newDrawingStrokes)     canvasView.drawing = newDrawing   } Not worked! Still blurred, just changed strokeWidth by multiply scale. 3: I try to reset PKDrawing or PKStroke transform by using scrollView scale. Then PKDrawing position disordered and it was still blurred. Please help me.
5
4
5.6k
Oct ’23
Swift PKLassoTool menu
In iOS whenever you use the PKLassoTool on a set of strokes if you click on the selected region a menu will pop up as you can see here Is there a way to add more buttons to this menu? I tried looking through the Swift docs but I haven't seen this mentioned anywhere.
0
1
607
Oct ’23
How to remove the Menu Interaction from PKCanvasView?
When using PencilKit and the default PKCanvasView, we get a pop-up menu with options like 'Select All | Insert Space' when we long tap on the canvas. What is the proper way to remove this menu completely? Thank you for your help.
Replies
0
Boosts
4
Views
794
Activity
Jun ’24
Apple Pencil Pro Squeeze API
I wonder if an Apple engineer could confirm: will the Apple Pencil Pro squeeze functionality be detectable in the current API, or will this be a future iPadOS extension to gesture recognizers / UIKit? I’d like to start playing with the functionality if it’s detected behind an existing event though. (Long press?)
Replies
1
Boosts
0
Views
1.3k
Activity
May ’24
PencilKit's PKToolPicker shows normally on iOS, but not on Mac Catalyst.
DemoCode: import SwiftUI import UIKit import PencilKit class PencilKitViewController: UIViewController, PKCanvasViewDelegate, PKToolPickerObserver {       lazy var canvasView: PKCanvasView = {     let canvasView = PKCanvasView()      canvasView.drawingPolicy = .anyInput      canvasView.translatesAutoresizingMaskIntoConstraints = false      return canvasView    }()       lazy var toolPicker: PKToolPicker = {     let toolPicker = PKToolPicker()     toolPicker.showsDrawingPolicyControls = true     toolPicker.addObserver(self)     return toolPicker   }()       let drawing = PKDrawing()       override func viewDidLoad() {     super.viewDidLoad()     canvasView.drawing = drawing     canvasView.delegate = self     view.addSubview(canvasView)   }       override func viewDidLayoutSubviews() {     super.viewDidLayoutSubviews()     canvasView.frame = view.bounds   }       override func viewDidAppear(_ animated: Bool) {     super.viewDidAppear(animated)     toolPicker.setVisible(true, forFirstResponder: canvasView)     toolPicker.addObserver(canvasView)     canvasView.becomeFirstResponder()   }       // canvas   func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) {     print("drawing")   }       func canvasViewDidFinishRendering(_ canvasView: PKCanvasView) {         }       func canvasViewDidEndUsingTool(_ canvasView: PKCanvasView) {         }       func canvasViewDidBeginUsingTool(_ canvasView: PKCanvasView) {         } } // UIRepresentable for SwiftUI struct PencilKitView: UIViewControllerRepresentable {       class Coordinator {     var parentObserver: NSKeyValueObservation?   }       var onSubmit: ((UIImage?, Error?) -> Void)? = .none       func makeUIViewController(context: Context) -> PencilKitViewController {     let pencilKitViewController = PencilKitViewController()     context.coordinator.parentObserver = pencilKitViewController.observe(\.parent, changeHandler: { vc, _ in      })     return pencilKitViewController   }       func updateUIViewController(_ uiViewController: PencilKitViewController, context: Context) {   }       func makeCoordinator() -> Self.Coordinator { Coordinator() } } struct ContentView: View {       var onSubmit: ((UIImage?, Error?) -> Void)? = .none       var body: some View {       PencilKitView()   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } } iOS: macCatalyst:
Replies
3
Boosts
0
Views
2.2k
Activity
Apr ’24
When PKCanvasView is first drawn, existing drawing objects disappear.
When PKCanvasView is first drawn, existing drawing objects disappear. PKDrawing, which previously had drawings saved with PKCanvasView, was saved as separate data or file. After that, while creating a new PKCanvasView, I loaded the saved PKDrawing and reflected it in the new PKCanvasView. Below is the code. canvasView!.drawing = draw! Previously saved text or lines will be displayed normally. However, when I draw with the pen for the first time (when I touch the screen with the pen), the old writing or lines disappear. And after I leave a line or text, if I pan across the screen, the old text or line appears again. If this phenomenon occurs and you touch the screen again with the fan, this phenomenon will no longer occur. This phenomenon occurs the first time when a new PKCanvasView is declared and the previously saved PKDrawing is reflected. Could you please help me with why this phenomenon occurs and how to improve it?
Replies
4
Boosts
0
Views
994
Activity
Apr ’24
Blurry and low resolution of PKCanvasView, as overlayview from PDFView.
Hi, I am trying to make a simple note taking app that users can draw something on pdfview with apple pencil. (I used PDFKit, PencilKit for the code.) I followed the instruction code of WWDC22's "What's new in PDFKit." - "overlayProvider" (so you can see the code at the video.) I was able to draw something each view of pdf page. But the issue was, the resolution of overlayview or subview of pdfview is low. As far as I know, the pkcanvasview draws vertor-based drawings. So I never thought the image or the lines I draw will be that blurry. Is this buggy or is this the normal thing? (+ I added a uibutton as subview of pdfview and the button also looks blurry.) I even tried to scale up the all the subviews when the subviews' layout is done, using contentScaleFactor. PKCanvasView inherits UIScrollView, so I enlarged the frame of pkcanvas view and fixed the scale to below 1.0. If the pkcanvasview looks blurry and that is because somewhat zoomed in wrong way, zooming out should be the solution. But, didn't work. Still blurry. and any other stuff like changing frame or size. So, anyone having same problem with me, or anyone can give me any solution. Please help me. I wish this is bug thing that can be fixed in any moment. -> This image is little bit zoomed in. but the drawing is blurry. and this is the normal pkcanvasview drawing, just subview of view(of VC).
Replies
3
Boosts
1
Views
1.4k
Activity
Mar ’24
PencilKit does not draw on PKCanvasView on VisionOS1.1 RC
Hi, I have an app using PencilKit that works on VisionOS 1.0. It means, a user can pick an inking tool from PKToolPicker and draw on PKCanvasView. The app is now on available on Vision Pro AppStore as well. However, when I test the app on VisionOS 1.1 RC Simulator, I can pick an inking tool but when I try to draw on the canvas, it just scrolls and no drawing appears on the PKCanvasView. I also noticed that the VisionOS 1.0 Simulator has the FreeForm app where you can draw with PencilKit but the VisionOS 1.1 RC Simulator does not have the FreeForm app. Is this a known issue? Will it be fixed before the release or is there a change in API so I can update the app accordingly? Thanks Zafer
Replies
1
Boosts
0
Views
911
Activity
Mar ’24
PKDrawing.image crashes on iOS 17 (EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000210)
In 2 days we have observed in Crashlytics over 50 crashes related to PKDrawing.image with EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000210 So far all on iPads with A10, A12 and A13; 100% on iOS 17 (17.1.1, 17.2, 17.3) (while in others the percent of iOS 17 was around 60-80%). Context: Images of the varying frame and scale resulting in screen resolution are being generated sequentially (in a background serial queue called almost one after another when requested), updating one CALayer.contents (and only after this update on Main Thread the next generation is allowed). One zoomable PKCanvasView is present on screen. The crashing line in code: let image = drawing.image(from: frame, scale: renderScale).cgImage The questions: Is there anything that can be done apart from throttling generation? Can the circumstances of the crash be determined – are there any indications accessible in code before calling PKDrawing.image that app might crash? The traces: 0 AGXMetalA12 AGX::BlitContext<AGX::G11::Encoders, AGX::G11::Classes, AGX::G11::ObjClasses>::copyTextureToBuffer(IOGPUMetalResource const*, unsigned long, unsigned long, unsigned long, AGXA12FamilyTexture*, unsigned int, unsigned int, MTLOrigin, MTLSize, unsigned long) + 96 9 PencilKit PKDrawing.image(from:scale:) + 28 0 AGXMetalA13 <redacted> + 96 9 PencilKit PKDrawing.image(from:scale:) + 28 0 AGXMetalA10 <redacted> + 72 9 PencilKit $s9PencilKit9PKDrawingV5image4from5scaleSo7UIImageCSo6CGRectV_12CoreGraphics7CGFloatVtF + 24
Replies
2
Boosts
1
Views
1.2k
Activity
Feb ’24
How do I make UIImage conform to Transferable?
Hello! I've been teaching myself Swift and wanted to challenge myself by creating a drawing app. I want to add a feature that allows the user to share the drawing on their canvas, but I'm having some difficulty. I tried using a ShareLink, but it's asking that I conform in to Transferable. How do I do that? import SwiftUI import PencilKit struct ContentView: View { @State private var canvasView = PKCanvasView() var body: some View { Text("Let's draw!") GeometryReader { geometry in VStack { Spacer() HStack { Spacer() PencilKitView() .frame(width: geometry.size.width * 1, height: geometry.size.height * 0.7) Spacer() } Spacer() } } //share button Button("share with a friend") { let portionRect = CGRect(x: 0, y: 0, width: 100, height: 100) let scale: CGFloat = 1.0 let image = canvasView.drawing.image(from: portionRect, scale: scale) ShareLink( item: image, preview: SharePreview( "Share Preview", image: image ) ) } } } struct PencilKitView: UIViewRepresentable { typealias UIViewType = PKCanvasView let toolPicker = PKToolPicker() func makeUIView(context: Context) -> PKCanvasView { let pencilKitCanvasView = PKCanvasView() pencilKitCanvasView.drawingPolicy = PKCanvasViewDrawingPolicy.anyInput toolPicker.addObserver(pencilKitCanvasView) toolPicker.setVisible(true, forFirstResponder: pencilKitCanvasView) pencilKitCanvasView.becomeFirstResponder() return pencilKitCanvasView } func updateUIView(_ uiView: PKCanvasView, context: Context) { } } #Preview { ContentView() }
Replies
1
Boosts
0
Views
2.4k
Activity
Feb ’24
PencilKit PKCanvasViewDelegate interface for drawing process callback
Is there any chance to modify the strokes of PKDrawing while drawing in on-process ? since I notice in canvasViewDrawingDidChange is called after drawing stroke is finished My objective is to get realtime feedback modification of PKStroke Thank you in advance
Replies
0
Boosts
0
Views
638
Activity
Feb ’24
PencilKit drawing quality
I've noticed in FreeForm app drawing stroke is very nice, like vector object, is there any chance to get the same result of drawing stroke quality like in the FreeForm app ?
Replies
0
Boosts
0
Views
698
Activity
Feb ’24
PencilKit help
I am making a code that uses pencilKit and you can draw on the canvas. However, I want to be able to detect if the resulted drawing is in contact with a Rectangle(). Is there any way to do this? I dont want to use variables for X and Y positions because I have more than 400 rectangles in a grid.
Replies
1
Boosts
0
Views
1.1k
Activity
Feb ’24
How to add UIImageView to PKDrawing and get it as data
I have a question about an app using PencilKit. I would like to add a UIImageView that is addSubviewed on top of PKCanvasView to PKDrawing and retrieve it as data along with other drawing information using dataRepresentation(). Please tell me how.
Replies
1
Boosts
0
Views
729
Activity
Jan ’24
How to add UIImageView to PKDrawing and get it as data
I have a question about an app using PencilKit. I would like to add a UIImageView that is addSubviewed on top of PKCanvasView to PKDrawing and retrieve it as data along with other drawing information using dataRepresentation(). Please tell me how.
Replies
0
Boosts
0
Views
554
Activity
Jan ’24
wwdc2022-10089 and various issues
Hi, I encounter various problems with inserting PKDrawing into a PDFAnnotation : First : After "page.addAnnotation(myCustomAnnotation)", saved document seems corrupted (affected pages are displayed with a "X" watermark covering the whole page), Second : The only way to extract PKDrawing from the annotation is unarchiveTopLevelObjectWithData: which is deprecated, Final : I'm not able to re-read PKDrawings to restore PKCanvasView undoManager. Does anyone have an idea on a correct way to do this? Thank you in advance and happy new year everyone!
Replies
0
Boosts
0
Views
709
Activity
Jan ’24
PKCanvasView: zoom+rotate gesture=ded performance
I'm trying to add rotation functionality to the PKCanvasView, so that rotation gesture rotates the drawing, I want it to be working together with zoom gesture too, which is already implemented in the PKCanvasView. Rotation works reasonably fine when rotation and zoom can't be performed simultaneously. The moment I enable simultaneous zoom+rotation performance of rotation gets noticably bad. Visually it looks like rotation only happens on noticable angles like 10,20,30.. degrees which looks choppy. Zoom at the same time looks smooth as before. I'm trying to understand why and how to fix this. The rotation gesture handler is here: @objc func handleRotation(_ gesture: UIRotationGestureRecognizer) { guard let curView = gesture.view as? PKCanvasView else {return} if gesture.state == .began || gesture.state == .changed { let rotation = gesture.rotation let gestureCenter = gesture.location(in: curView) let center = CGPoint(x: gestureCenter.x / curView.zoomScale, y: gestureCenter.y / curView.zoomScale) let finalTransform = CGAffineTransform(translationX: -center.x, y: -center.y) .concatenating(CGAffineTransform(rotationAngle: rotation)) .concatenating(CGAffineTransform(translationX: center.x, y: center.y)) curView.drawing.transform(using: finalTransform) gesture.rotation = 0 } } which just constructs rotation matrix around rotation gesture center and calls curView.drawing.transform(using: finalTransform). Rotation matrix is correct I think because without simultaneous zoom+rotation it rotates everything as intended and smoothly under any zoom. (Just for clarity: in the rotation handler the matrix does only the rotation, zoom is done in pinch gesture handler implemented by the PKCanvasView. The zoomScale is used in estimations only to figure out the content coodrinate). The handleRotation method is set as let rotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(handleRotation)) rotationGestureRecognizer.delegate = self canvasView.addGestureRecognizer(rotationGestureRecognizer) The simultaneous handling of zoom+rotation I enable with this code: func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } What I've tried: Tried to nable simultaneous recognition only for zoom+rotation (without the pan gesture and possible others), the effect is the same. For example rotation+pan work smoothly together. I've verified that the gesture handler is called very often, every 8-16 milliseconds (rotation matrix creation and curView.drawing.transform(using: finalTransform) is included in this time period) which should be enough for visually smooth rotation. Other observations: When zoom and rotation both are enabled the rotation sometimes gets completely stuck, zoom and pinch is responsive but rotation is not happening visually. Only when I release fingers from the screen the rotation unstucks and content is rotated as if it didn't stuck at all. During the "stuck" period the rotation handler is called with the same frequency it's just not being visually reflected. Other thoughts: It kind of looks like the tranformation I set in the rotation handler gets overwritten by transformation from the zoom handler. But pan+rotation works fine together which confuses this theory.
Replies
1
Boosts
0
Views
889
Activity
Dec ’23
SwiftUI Third Party Keyboard
Hello. I am developing an app that helps people with language issues communicate and for that I thought I could do a custom keyboard for them. I was wondering if there is a way of doing a 3rd party keyboard using SwiftUI (little to no UIKit) and if so, where can I find documentation/tutorials. I have looked with no success. Additionally, can we use a PKCanvasView inside of keyboards (ie, is there any restriction/policy)? I intend to use it so that users can draw the words they're looking for (I already have the model and know how to come up with the suggestions and everything) Thanks a lot!
Replies
0
Boosts
0
Views
798
Activity
Nov ’23
Lasso no longer works with iOS17
Since updating to iOS17, lasso no longer works in Pencilkit with SwiftUI support using UIViewRepresentable. It works in PKToolPicker, but the programmatically created PKLassoTool does not work. How can I get lasso to work?
Replies
6
Boosts
4
Views
2.1k
Activity
Oct ’23
Editing in duplicate page is affecting original Page iOS17
I am showing PDF file in QLPreview Controller, in iOS17, after copy and paste the page of pdf and edit the original page using Pencil kit, will affect the duplicate page also. How to restrict? Thanks to all
Replies
0
Boosts
0
Views
651
Activity
Oct ’23
PencilKit: zoom PKCanvasView, drawing blurred (swift, iOS 15)
It seems PKCanvasView overrides the property UIScrollViewDelegate which inherits from the UIScrollView to PKCanvasViewDelegate. And does not provide access to UIScrollViewDelegate. In order to implement zooming, so I added a PKCanvasView into my own UIScrollView. And implemented delegate method viewForZooming in which return PKCanvasView. But all drawing in PKCanvasView was blurred when zooming or scale. How to re-render drawing after zoom to make it has reasonable stroke width and clear ? Some related code: let canvasView = PKCanvasView() let scrollView = UIScrollView() override func viewDidLoad() {     super.viewDidLoad()     self.view.addSubview(scrollView)     scrollView.addSubview(canvasView)     scrollView.delegate = self     scrollView.minimumZoomScale = 0.5     scrollView.maximumZoomScale = 2.5 } func viewForZooming(in scrollView: UIScrollView) -> UIView? {     return canvasView } Some solutions I had tried: 1: Reset PKCanvasView contentScaleFactor func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {     if let canvas = view {       let contentScale = scale * UIScreen.main.scale       canvas.contentScaleFactor = contentScale     }   } Not worked! 2: Re-render PKStroke: func reRender(_ scale: CGFloat) {     let newStrokeWidth = strokeWidth * scale     var newDrawingStrokes: [PKStroke] = []     for stroke in canvasView.drawing.strokes {       canvasView.tool = PKInkingTool(.pen, color: .red, width: newStrokeWidth)       var newPoints = [PKStrokePoint]()       stroke.path.forEach { (point) in         let newPoint = PKStrokePoint(location: point.location,                        timeOffset: point.timeOffset,                        size: CGSize(width: newStrokeWidth, height: newStrokeWidth),                        opacity: CGFloat(1), force: point.force,                        azimuth: point.azimuth, altitude: point.altitude)         newPoints.append(newPoint)       }       let newPath = PKStrokePath(controlPoints: newPoints, creationDate: Date())       let newStroke = PKStroke(ink: PKInk(.pen, color: UIColor.red), path: newPath)       newDrawingStrokes.append(newStroke)     }     let newDrawing = PKDrawing(strokes: newDrawingStrokes)     canvasView.drawing = newDrawing   } Not worked! Still blurred, just changed strokeWidth by multiply scale. 3: I try to reset PKDrawing or PKStroke transform by using scrollView scale. Then PKDrawing position disordered and it was still blurred. Please help me.
Replies
5
Boosts
4
Views
5.6k
Activity
Oct ’23
Swift PKLassoTool menu
In iOS whenever you use the PKLassoTool on a set of strokes if you click on the selected region a menu will pop up as you can see here Is there a way to add more buttons to this menu? I tried looking through the Swift docs but I haven't seen this mentioned anywhere.
Replies
0
Boosts
1
Views
607
Activity
Oct ’23