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.

PencilKit Documentation

Posts under PencilKit tag

49 Posts
Sort by:
Post not yet marked as solved
1 Replies
698 Views
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.
Posted
by M-Three.
Last updated
.
Post not yet marked as solved
0 Replies
235 Views
Many users reported my app use too much energy. I have profiled the app and find out the problem is from PencilKit. It only happens on a canvas with more than 5000 strokes: When user use pen or any ink tool to draw something on the screen and leave the pencil off from the screen, the CPU go immediately to 100-165% for 3 seconds then go back to 0%. I have time profiled CPU and found out that 90% of the calculation are from CoreHandwriting. I suppose each time when user did draw something on the screen, PencilKit will try to recognize text from the handwriting in the background. That leads to high CPU usage. If this is true, I hope there will be an option to disable the CoreHandwriting. Otherwise I hope PencilKit will be updated to consume less energy, because my app received many bad reviews because of this problem and I can't do anything.
Posted
by Motconvi.
Last updated
.
Post not yet marked as solved
0 Replies
343 Views
I'm developing an app that allows you to view PDFs (music notes). I'm using PDFkit to display the pdf and need the pencilkit functionality while viewing the pdf (in the same way that you could mark up a book you are reading in iBooks when you click on the "Mark up" button). I am able to add a canvas on top of the pdf view however, I cannot save the canvas to be merged with the PDF. QuickLook offers a solution because the pencilkit looks to be built into it and works perfectly just the way I need it to, however, I don't need the other things that come with QuickLook. I desperately need your help with how I can get the pencilkit functionality on my custom PDF viewing app. At this point, I can't even find a solution for the "Mark up" button (pencil.tip.crop.circle) to be filled when tapped. Thank you in advance!
Posted
by Mikul29.
Last updated
.
Post not yet marked as solved
0 Replies
219 Views
I thought that PencilKit's PKPoint force was in a some kind of interval. However I don't find anywhere a max value for this pressure property. Is there a max value? If not, how could I know the almost max pressure level value?
Posted
by bellots.
Last updated
.
Post not yet marked as solved
0 Replies
226 Views
I've got a question about PKPoint processing. I've got an application where I have to get both image and points which generated this image. However I've checked that points have been already interpolated, so there's no way to get raw points (which I thought should be similar to display's refresh rate x seconds requested to draw). Is there a way to get raw points (not processed) and not interpolated points?
Posted
by bellots.
Last updated
.
Post not yet marked as solved
0 Replies
246 Views
Hi. My goal is to convert PKDrawing to NSImage on Mac Server. For this, I use Vapor framework and receive POST request with PKDraiwng's base64 string(from PKDrawing.dataRepresentaion()). However, this task succeeds on local device, but fails on Remote Mac server. (with exact same logic & data) In failing cases, input base64 string was successfully converted to PKDrawing, but PKDrawing to NSImage converting was failed. (NSImage object is located at 0x000000 memory address) Is there any hardware dependencies to convert PKDrawing to NSImage? Please give me some help. let base64String = "d3Jk8AEACAASEAAAAAAAAAAAAAAAAAAAAAASECTanLurR0TesQ6DbJmhcB4aBggAEAAYABoGCAEQARgBIi4KFA39/Hw/FcXERD4dhYSEPiXNzEw/EhRjb20uYXBwbGUuaW5rLm1hcmtlchgDKuMHChDT0UH2JnhFKJXSKOjPQ4MBEgYIABABGAEaBggAEAEYACAAKp8HChD+PS4PhSlOD4NNWagXDeasEXgNipZotcNBGCwg4wMoHDIIAABcQegDAAA68AZww4BBYPKVQgAAAADNABSQwlXQOFhkkEEAiZhCaJFtPSkB0o7CVQhBMDaaQcBDlkLn+6k9OAHOjsJVX0IQuaRBUDOSQs3MzD04Ac6OwlVfQvA7r0HQS41CQmDlPTABzo7CVV9CsPq2QYC7iEKPwvU9JgHOjsJVqUGArrtBYPaFQgAAAD4mAc6OwlXCQOCNv0GQCINCkxgEPiYBzo7CVcJAQG3DQSDkf0InMQg+JwHOjsJV2kAIcMdBwAh5QrpJDD4qAc6OwlUoQWCeykEA3HFCTmIQPi4Bzo7CVX1BGD/NQeBdakLhehQ+MwHOjsJV80HYkNBBQJphQpqZGT45Ac6OwlV5QoDP0UEgHFpCLbIdPj0Bzo7CVdBCkDHTQQCeUkLByiE+QAHOjsJVGEM4cNRB4MJKQlTjJT5BAc6OwlU/Q0jS1UHARENC5/spPkMBzo7CVVtD6F/WQUB1O0J7FC4+QwHOjsJVW0MAc9hBQAMyQjMzMz5DAc6OwlVbQ6ix2UEghSpCx0s3PkMBzo7CVVtDuBPbQQAHI0JaZDs+QwHOjsJVW0NoA91BQNobQu58Pz5DAc6OwlVbQyCk30GAChVCgZVDPkMBzo7CVVtDeNLiQYCADkIUrkc+QwHOjsJVW0M4JOZBYAIHQs3MTD5DAc6OwlVbQ1hV7UFA2/lB9P1UPkMBzo7CVVtDgDf1QUD35kEbL10+QwHmjsJVW0PYGwFCwM7OQfp+aj5DAamPWVVbQ8y1BkKA0r9BtMh2PkMBJJD/VFtDkJYMQgC+tEFKDII+QwGIkMZUW0OwxxNCgDOwQXE9ij4/AeCQtFRbQ6jwG0IAebFBqvGSPjgB/ZC0VAdDyCEjQkDqvUGamZk+OAH9kLRUW0JsqypCACzOQXe+nz44Af2QtFRbQtzsL0JA49lBCtejPjgB/ZC0VFtCHHU1QoCa5UGwcqg+OAH9kLRUbUKMtjpCQAzwQUSLrD4+Af2QtFTqQnifP0KAfvhB16OwPkYB/ZC0VJ1DaBdHQoDJAULHS7c+UQH9kLRUoUQ0qU1CIL0EQqRwvT5XAf2QtFQtRXziU0JADgZCkxjEPlkB/ZC0VFdFgEtfQkCxBUJg5dA+WQH9kLRUV0XQNWZC4BoCQj0K1z7zAP2QtFRXRWRdbELAIPtB46XbPkwA/ZC0VD4tMhQNAACgQBUAAGBBHQAAhEIlAACMQkDAn9OfkwU6BggAEAAYAEIQxjieV95cTuaAtuVXCPxhZA==" let drawingData: PKDrawing = try! .init(data: Data(base64Encoded: base64String)!) let image = drawingData.image(from: drawingData.bounds, scale: 2) // -> fails
Posted
by hanung.
Last updated
.
Post not yet marked as solved
0 Replies
235 Views
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.
Posted
by kyroneus.
Last updated
.
Post not yet marked as solved
2 Replies
589 Views
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.
Posted
by tobaiin.
Last updated
.
Post not yet marked as solved
0 Replies
286 Views
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 ?
Posted Last updated
.
Post not yet marked as solved
2 Replies
457 Views
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!
Posted Last updated
.
Post not yet marked as solved
0 Replies
339 Views
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
Posted
by Prorobot.
Last updated
.
Post not yet marked as solved
0 Replies
224 Views
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
Posted Last updated
.
Post not yet marked as solved
0 Replies
247 Views
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!
Posted
by D-er.
Last updated
.
Post not yet marked as solved
2 Replies
1.1k Views
There are use cases where someone who's using an Apple Pencil may not want to enter text via Scribble. A simple example is writing "UIViewController" in a text view is unlikely to be successful. I'd like to disable Scribble in this case and let the keyboard become the input mechanism. (Disabling Scribble system-wide in Settings is both cumbersome and overkill.) The closest I can come to making this happen is by adding a UIScribbleInteraction on a UITextView and returning false when scribbleInteraction(shouldBeginAt:) is called. This disables Scribble on the text view, and prevents writing from being converted into text, but the input widget still appears on screen and isn't very useful. Here is a sample project that demonstrates the problem: http://files.iconfactory.net/craig/bugs/Scribbler.zip Hopefully, I'm doing something wrong here. If not, I'm happy to submit this as a FB. -ch
Posted Last updated
.
Post not yet marked as solved
2 Replies
384 Views
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,
Posted Last updated
.
Post not yet marked as solved
1 Replies
582 Views
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
Posted
by uson1x.
Last updated
.