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

133 Posts

Post

Replies

Boosts

Views

Activity

Apple Pencil azimuth not working
I have a drawing app that I created and have sold on the App Store since 2018. It requires an Apple Pencil. My app uses the azimuth feature to orient the brush pattern that is drawn on the screen. A user just contacted me and said his azimuth feature is not working. Now it works fine for me, and I have not heard complaints from other users, so this leads me to believe it could be a problem with his Pencil. I wanted to show my code just to rule out that the problem is on my end and also get suggestions on what I should tell this user to do. Should he just contact Apple about it?                      // calculate the pencil direction let vector1 = touch.azimuthUnitVector(in: selectedCanvas) let angle = atan2(vector1.dy, vector1.dx) let azimu = angle - CGFloat(Double.pi) / 2.0 // adjust for wonky azimuth rotation translation if azimu >= -4.5 && azimu <= 0 {       rot = abs(azimu) } else {        rot = 6.2 - azimu } The azimu is then used as a point rotation in a basic Bézier curve and drawn on the screen. If it works on one iPad, it should work on all of them, right? He’s using an iPad Pro and so am I. I asked if he was using an Apple Pencil and he said yes.
0
0
823
Nov ’21
PKStrokePont issue (iOS15.0, Xcode13.0)
I would like to adjust the PKStrokePoint of the canvas drawing in the PencilKit API. The code below should generate the same strokes, but the drawing doesn't generate well depending on the type of pen selected. func generate_sameDrawing(drawing :PKDrawing) -> PKDrawing{     var newDrawingStrokes : [PKStroke] = []           for stroke in drawing.strokes {       var newPoints : [PKStrokePoint] = []       stroke.path.forEach { (point) in         let newPoint = PKStrokePoint(location: point.location,                        timeOffset: point.timeOffset,                        size: point.size,                        opacity: point.opacity,                        force: point.force,                        azimuth: point.azimuth,                        altitude: point.altitude)         newPoints.append(newPoint)       }       let newPath = PKStrokePath(controlPoints: newPoints, creationDate: Date())       let newStroke = PKStroke(ink: PKInk(stroke.ink.inkType, color: stroke.ink.color), path: newPath)                 newDrawingStrokes.append(newStroke)             }     let newDrawing = PKDrawing(strokes: newDrawingStrokes)     return newDrawing   } Here is an image showing the result. The pen and marker generate exactly the same drawing, but with pencil type, the thickness and opacity of the stroke change, and the stroke location also moves a little. If you find any bugs/mistakes do let me know.
2
0
1.5k
Nov ’21
QuickLook Preview saving errors or no saving
Hi together, I am developing a document viewer for a specific API. I download the relevant files to a custom directory and open them using a QLPreviewController in SwiftUI. I built this with a UIViewControllerRepresentable. Everything is working fine except the saving of modified files using the pencil markup in the preview. Here is the error: https://pastebin.com/TRnfduE5 This is how my controller looks like: struct PreviewController: UIViewControllerRepresentable {     let url: URL     @Binding var isPresented: Bool          func makeUIViewController(context: Context) -> UINavigationController {         let controller = QLPreviewController()         controller.dataSource = context.coordinator         controller.navigationItem.leftBarButtonItem = UIBarButtonItem(                     barButtonSystemItem: .done, target: context.coordinator,                     action: #selector(context.coordinator.dismiss)                 )                  let navigationController = UINavigationController(rootViewController: controller)         return navigationController     }          func makeCoordinator() -> Coordinator {         return Coordinator(parent: self)     }          func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {             let controller = QLPreviewController()             controller.dataSource = context.coordinator         }          class Coordinator: QLPreviewControllerDataSource {                  let parent: PreviewController                  init(parent: PreviewController) {             self.parent = parent         }                  @objc func dismiss() {             parent.isPresented = false         }                  func numberOfPreviewItems(             in controller: QLPreviewController         ) -> Int {             return 1         }                  func previewController(             _ controller: QLPreviewController, previewItemAt index: Int         ) -> QLPreviewItem {             return parent.url as QLPreviewItem         }                  func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem         ) -> QLPreviewItemEditingMode {             .createCopy         }                  func previewController(_: QLPreviewController, didUpdateContentsOf: QLPreviewItem){             print("Updated.")         }                  func previewController(_: QLPreviewController, didSaveEditedCopyOf: QLPreviewItem, at: URL) {             print("Saved: " + at.path)         }              } } Does anyone know, what the problem is here? And I made another experience: as you see there, I'm currently only logging the output / actions - and there isn't anything being logged, if I made just one edit. It only throws the error above after the 1st edit - so if I edit sth again and tap done or the pencil icon again... Is this ok so? Thanks for any help or advise!
2
0
1.8k
Nov ’21
Data Communication between Unity and NativeiOS
Hello! I am working on an app that uses a native iOS as a base to show a 3D Room in Unity. I archive this by using: https://docs.unity3d.com/Manual/UnityasaLibrary-iOS.html I want to use iOS native because of the pencilKit support. I am now looking for the best option to share data between the two instances. I am not sure if the better way is to just let the native Swift App talk to a Realm and just send in the data I want to store from Unity to Swift, or if its possible to let both apps talk to the same database. The goal is to create a 3D Object in Unity (that has some properties like coordinates) and assign a PKDrawingFile to it. Thank you for all your help, Jakob
0
0
1.3k
Nov ’21
Remove or hide thumbnail bar from QLpreviewcontroller in iOS 13
How to remove or hide thumbnail bar from QLPreviewcontroller in iOS 13? Based of our requirement we have to block delete pdf page option. So I tried to hide or remove thumbnail pdf bar, but I didn't get any solution. Basically I need markup tool all features(like drawing, signature, add text and add shape) to edit without allowing to page deletion. Please guide me to achieve this Thanks & Regards Ponlingam S
0
0
815
Nov ’21
How to recognize handwritten text like the Notes app?
In WWDC-20 Inspect, modify, and construct PencilKit drawings, Will Thimbleby said: Spline-based recognition can make use of maskedPathRanges to provide a sensible interpretation of masked strokes, and this is what we do for handwriting recognition in Notes. If I have a PKCanvas and make a PKDrawing with Apple Pencil, how can I convert these PKStrokePaths: into str = "Hello, World!" Can this be done using Apple frameworks? Cheers!
0
0
984
Nov ’21
PDF Broken issue iOS 15.0
H i, I have used the PKCanvas view in my app. writing something in my PKCanvas View then convert it into a PDF. But adding the header for each page in PDF by using the following code,   let printable:CGRect = CGRect(x: 0, y: 50, width: 595 , height: 841)       render.setValue(NSValue(cgRect: page), forKey: "paperRect")       render.setValue(NSValue(cgRect: printable), forKey: "printableRect")       // 4. Create PDF context and draw       let pdfData = NSMutableData()       UIGraphicsBeginPDFContextToData(pdfData, CGRect(x:0,y:0,width: 595.2,height:841), nil)       for i in 1...render.numberOfPages {           UIGraphicsBeginPDFPage();           let bounds = UIGraphicsGetPDFContextBounds()           render.drawPage(at: i-1, in: bounds)       }       UIGraphicsEndPDFContext(); But while renderer the PKCanvas View it was broken, I think PKCanvas view rendering is not properly working in iOS 15.0,15.0.1 and 15.0.2. I have attached the following screenshot,
2
0
1.1k
Oct ’21
How to make PKInkingTool width constant?
We want to use new iOS 14 PencilKit vector capabilities in our app. However, our current drawing component is always fixed-width. If we use dynamic brush width, like PencilKit has, it will look different on our other platforms. Is there a way to make PKInkingTool having fixed width? I tried swizzling methods like defaultWidthForInkType:, but my swizzled methods are never called. @implementation PKInkingTool (Tracking) (void)load {     [super load];     static dispatch_once_t onceToken;     dispatch_once(&amp;onceToken, ^{         const char *className = [NSStringFromClass(self) UTF8String];         Class class = objc_getMetaClass(className);         SEL originalSelector = @selector(defaultWidthForInkType:);         SEL swizzledSelector = @selector(ed_defaultWidthForInkType:);         Method originalMethod = class_getClassMethod(class, originalSelector);         Method swizzledMethod = class_getClassMethod(class, swizzledSelector);         BOOL didAddMethod =             class_addMethod(class,                 originalSelector,                 method_getImplementation(swizzledMethod),                 method_getTypeEncoding(swizzledMethod));         if (didAddMethod) {             class_replaceMethod(class,                 swizzledSelector,                 method_getImplementation(originalMethod),                 method_getTypeEncoding(originalMethod));         } else {             method_exchangeImplementations(originalMethod, swizzledMethod);         } } (CGFloat)ed_defaultWidthForInkType:(PKInkType)inkType {     return 15.0; } @end
1
0
1.4k
Sep ’21
Xcode 13 beta Error with Pencil kit
There is error when launch app which use Pencil kit When I try with iOS15 simulator or Xcode editor preview it crashed Here is error message Library not loaded: /usr/lib/swift/libswiftPencilKit.dylibI Reason: tried: '/Users/.../Products/Debug-iphonesimulator/libswiftPencilKit.dylib' (no such file) There is no problem with iOS14.5 simulator
3
0
1.7k
Sep ’21
PencilKitCanvas cannot becomeFirstResponder in SwiftUI
I have done the same thing in SwiftUI using UIViewRepresentable, but toolPicker doesn't show so I checked isFirstResponder property and I found that it was still false after I called canvas.becomeFirstResponder(). Check this out: struct NoteCanvasView: UIViewRepresentable {     func makeUIView(context: Context) -> PKCanvasView {         let canvas = PKCanvasView()         canvas.drawingPolicy = .anyInput         canvas.delegate = context.coordinator.self                  let toolPicker = PKToolPicker()         toolPicker.setVisible(true, forFirstResponder: canvas)         toolPicker.addObserver(canvas)         print(canvas.canBecomeFirstResponder)         canvas.becomeFirstResponder()         print(canvas.isFirstResponder)         return canvas     }          func updateUIView(_ canvas: PKCanvasView, context: Context) {         canvas.becomeFirstResponder()     }          func makeCoordinator() -> Coordinator {         Coordinator(self)     }          class Coordinator: NSObject {         var parent: NoteCanvasView         init(_ parent: NoteCanvasView) {             self.parent = parent         }     } } I found canvas.canBecomeFirstResponder returns true and canvas.isFirstResponder always returns false. Is this a bug in current version of SwiftUI??
0
0
791
Aug ’21
Apple Pencil azimuth not working
I have a drawing app that I created and have sold on the App Store since 2018. It requires an Apple Pencil. My app uses the azimuth feature to orient the brush pattern that is drawn on the screen. A user just contacted me and said his azimuth feature is not working. Now it works fine for me, and I have not heard complaints from other users, so this leads me to believe it could be a problem with his Pencil. I wanted to show my code just to rule out that the problem is on my end and also get suggestions on what I should tell this user to do. Should he just contact Apple about it?                      // calculate the pencil direction let vector1 = touch.azimuthUnitVector(in: selectedCanvas) let angle = atan2(vector1.dy, vector1.dx) let azimu = angle - CGFloat(Double.pi) / 2.0 // adjust for wonky azimuth rotation translation if azimu >= -4.5 && azimu <= 0 {       rot = abs(azimu) } else {        rot = 6.2 - azimu } The azimu is then used as a point rotation in a basic Bézier curve and drawn on the screen. If it works on one iPad, it should work on all of them, right? He’s using an iPad Pro and so am I. I asked if he was using an Apple Pencil and he said yes.
Replies
0
Boosts
0
Views
823
Activity
Nov ’21
PKStrokePont issue (iOS15.0, Xcode13.0)
I would like to adjust the PKStrokePoint of the canvas drawing in the PencilKit API. The code below should generate the same strokes, but the drawing doesn't generate well depending on the type of pen selected. func generate_sameDrawing(drawing :PKDrawing) -> PKDrawing{     var newDrawingStrokes : [PKStroke] = []           for stroke in drawing.strokes {       var newPoints : [PKStrokePoint] = []       stroke.path.forEach { (point) in         let newPoint = PKStrokePoint(location: point.location,                        timeOffset: point.timeOffset,                        size: point.size,                        opacity: point.opacity,                        force: point.force,                        azimuth: point.azimuth,                        altitude: point.altitude)         newPoints.append(newPoint)       }       let newPath = PKStrokePath(controlPoints: newPoints, creationDate: Date())       let newStroke = PKStroke(ink: PKInk(stroke.ink.inkType, color: stroke.ink.color), path: newPath)                 newDrawingStrokes.append(newStroke)             }     let newDrawing = PKDrawing(strokes: newDrawingStrokes)     return newDrawing   } Here is an image showing the result. The pen and marker generate exactly the same drawing, but with pencil type, the thickness and opacity of the stroke change, and the stroke location also moves a little. If you find any bugs/mistakes do let me know.
Replies
2
Boosts
0
Views
1.5k
Activity
Nov ’21
Apple I Pencil Not Connected
I have 1st gen I Pencil, After connected once its show Not connected. Then Tap for Connect its show long time for failed or out of range or make sure accessories turn on but what is the problem i couldn't understand and how can i repair it ?
Replies
0
Boosts
0
Views
722
Activity
Nov ’21
QuickLook Preview saving errors or no saving
Hi together, I am developing a document viewer for a specific API. I download the relevant files to a custom directory and open them using a QLPreviewController in SwiftUI. I built this with a UIViewControllerRepresentable. Everything is working fine except the saving of modified files using the pencil markup in the preview. Here is the error: https://pastebin.com/TRnfduE5 This is how my controller looks like: struct PreviewController: UIViewControllerRepresentable {     let url: URL     @Binding var isPresented: Bool          func makeUIViewController(context: Context) -> UINavigationController {         let controller = QLPreviewController()         controller.dataSource = context.coordinator         controller.navigationItem.leftBarButtonItem = UIBarButtonItem(                     barButtonSystemItem: .done, target: context.coordinator,                     action: #selector(context.coordinator.dismiss)                 )                  let navigationController = UINavigationController(rootViewController: controller)         return navigationController     }          func makeCoordinator() -> Coordinator {         return Coordinator(parent: self)     }          func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {             let controller = QLPreviewController()             controller.dataSource = context.coordinator         }          class Coordinator: QLPreviewControllerDataSource {                  let parent: PreviewController                  init(parent: PreviewController) {             self.parent = parent         }                  @objc func dismiss() {             parent.isPresented = false         }                  func numberOfPreviewItems(             in controller: QLPreviewController         ) -> Int {             return 1         }                  func previewController(             _ controller: QLPreviewController, previewItemAt index: Int         ) -> QLPreviewItem {             return parent.url as QLPreviewItem         }                  func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem         ) -> QLPreviewItemEditingMode {             .createCopy         }                  func previewController(_: QLPreviewController, didUpdateContentsOf: QLPreviewItem){             print("Updated.")         }                  func previewController(_: QLPreviewController, didSaveEditedCopyOf: QLPreviewItem, at: URL) {             print("Saved: " + at.path)         }              } } Does anyone know, what the problem is here? And I made another experience: as you see there, I'm currently only logging the output / actions - and there isn't anything being logged, if I made just one edit. It only throws the error above after the 1st edit - so if I edit sth again and tap done or the pencil icon again... Is this ok so? Thanks for any help or advise!
Replies
2
Boosts
0
Views
1.8k
Activity
Nov ’21
Data Communication between Unity and NativeiOS
Hello! I am working on an app that uses a native iOS as a base to show a 3D Room in Unity. I archive this by using: https://docs.unity3d.com/Manual/UnityasaLibrary-iOS.html I want to use iOS native because of the pencilKit support. I am now looking for the best option to share data between the two instances. I am not sure if the better way is to just let the native Swift App talk to a Realm and just send in the data I want to store from Unity to Swift, or if its possible to let both apps talk to the same database. The goal is to create a 3D Object in Unity (that has some properties like coordinates) and assign a PKDrawingFile to it. Thank you for all your help, Jakob
Replies
0
Boosts
0
Views
1.3k
Activity
Nov ’21
Remove or hide thumbnail bar from QLpreviewcontroller in iOS 13
How to remove or hide thumbnail bar from QLPreviewcontroller in iOS 13? Based of our requirement we have to block delete pdf page option. So I tried to hide or remove thumbnail pdf bar, but I didn't get any solution. Basically I need markup tool all features(like drawing, signature, add text and add shape) to edit without allowing to page deletion. Please guide me to achieve this Thanks & Regards Ponlingam S
Replies
0
Boosts
0
Views
815
Activity
Nov ’21
How to recognize handwritten text like the Notes app?
In WWDC-20 Inspect, modify, and construct PencilKit drawings, Will Thimbleby said: Spline-based recognition can make use of maskedPathRanges to provide a sensible interpretation of masked strokes, and this is what we do for handwriting recognition in Notes. If I have a PKCanvas and make a PKDrawing with Apple Pencil, how can I convert these PKStrokePaths: into str = "Hello, World!" Can this be done using Apple frameworks? Cheers!
Replies
0
Boosts
0
Views
984
Activity
Nov ’21
A product suggestion to Apple Pencil: Use the other end as eraser
It will be more natural if the other side of pencil can be used as eraser instead of having to choose eraser from toolbar (even double click on pencil).
Replies
0
Boosts
0
Views
491
Activity
Nov ’21
PDF Broken issue iOS 15.0
H i, I have used the PKCanvas view in my app. writing something in my PKCanvas View then convert it into a PDF. But adding the header for each page in PDF by using the following code,   let printable:CGRect = CGRect(x: 0, y: 50, width: 595 , height: 841)       render.setValue(NSValue(cgRect: page), forKey: "paperRect")       render.setValue(NSValue(cgRect: printable), forKey: "printableRect")       // 4. Create PDF context and draw       let pdfData = NSMutableData()       UIGraphicsBeginPDFContextToData(pdfData, CGRect(x:0,y:0,width: 595.2,height:841), nil)       for i in 1...render.numberOfPages {           UIGraphicsBeginPDFPage();           let bounds = UIGraphicsGetPDFContextBounds()           render.drawPage(at: i-1, in: bounds)       }       UIGraphicsEndPDFContext(); But while renderer the PKCanvas View it was broken, I think PKCanvas view rendering is not properly working in iOS 15.0,15.0.1 and 15.0.2. I have attached the following screenshot,
Replies
2
Boosts
0
Views
1.1k
Activity
Oct ’21
How to make PKInkingTool width constant?
We want to use new iOS 14 PencilKit vector capabilities in our app. However, our current drawing component is always fixed-width. If we use dynamic brush width, like PencilKit has, it will look different on our other platforms. Is there a way to make PKInkingTool having fixed width? I tried swizzling methods like defaultWidthForInkType:, but my swizzled methods are never called. @implementation PKInkingTool (Tracking) (void)load {     [super load];     static dispatch_once_t onceToken;     dispatch_once(&amp;onceToken, ^{         const char *className = [NSStringFromClass(self) UTF8String];         Class class = objc_getMetaClass(className);         SEL originalSelector = @selector(defaultWidthForInkType:);         SEL swizzledSelector = @selector(ed_defaultWidthForInkType:);         Method originalMethod = class_getClassMethod(class, originalSelector);         Method swizzledMethod = class_getClassMethod(class, swizzledSelector);         BOOL didAddMethod =             class_addMethod(class,                 originalSelector,                 method_getImplementation(swizzledMethod),                 method_getTypeEncoding(swizzledMethod));         if (didAddMethod) {             class_replaceMethod(class,                 swizzledSelector,                 method_getImplementation(originalMethod),                 method_getTypeEncoding(originalMethod));         } else {             method_exchangeImplementations(originalMethod, swizzledMethod);         } } (CGFloat)ed_defaultWidthForInkType:(PKInkType)inkType {     return 15.0; } @end
Replies
1
Boosts
0
Views
1.4k
Activity
Sep ’21
Xcode 13 beta Error with Pencil kit
There is error when launch app which use Pencil kit When I try with iOS15 simulator or Xcode editor preview it crashed Here is error message Library not loaded: /usr/lib/swift/libswiftPencilKit.dylibI Reason: tried: '/Users/.../Products/Debug-iphonesimulator/libswiftPencilKit.dylib' (no such file) There is no problem with iOS14.5 simulator
Replies
3
Boosts
0
Views
1.7k
Activity
Sep ’21
How to make PKCanvasView and TextField inline?
I am working on an app that allow user to taking notes, and I want to support inline editing that means users can use PencilKit feature and edit text in a single note just like the Notes app. Is there any good idea to achieve this using SwiftUI?
Replies
0
Boosts
0
Views
892
Activity
Aug ’21
PencilKitCanvas cannot becomeFirstResponder in SwiftUI
I have done the same thing in SwiftUI using UIViewRepresentable, but toolPicker doesn't show so I checked isFirstResponder property and I found that it was still false after I called canvas.becomeFirstResponder(). Check this out: struct NoteCanvasView: UIViewRepresentable {     func makeUIView(context: Context) -> PKCanvasView {         let canvas = PKCanvasView()         canvas.drawingPolicy = .anyInput         canvas.delegate = context.coordinator.self                  let toolPicker = PKToolPicker()         toolPicker.setVisible(true, forFirstResponder: canvas)         toolPicker.addObserver(canvas)         print(canvas.canBecomeFirstResponder)         canvas.becomeFirstResponder()         print(canvas.isFirstResponder)         return canvas     }          func updateUIView(_ canvas: PKCanvasView, context: Context) {         canvas.becomeFirstResponder()     }          func makeCoordinator() -> Coordinator {         Coordinator(self)     }          class Coordinator: NSObject {         var parent: NoteCanvasView         init(_ parent: NoteCanvasView) {             self.parent = parent         }     } } I found canvas.canBecomeFirstResponder returns true and canvas.isFirstResponder always returns false. Is this a bug in current version of SwiftUI??
Replies
0
Boosts
0
Views
791
Activity
Aug ’21