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
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(&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
Post not yet marked as solved
1 Replies
251 Views
I connected my pencil to charge but the number didn’t change even after a while. I forget/reconnect it, and the battery was full (it was 34% before). Why did that happen?
Posted
by
Post not yet marked as solved
1 Replies
638 Views
I want to create a flood fill (aka paint bucket) tool with PencilKit. I am aware of the way to inspect the PKStrokes to identify an enclosed space to fill inside of them but I'm not sure the way to create the internal shape inside that space. Any suggestion or leads would be appreciated. Thanks in advance and please have a beautiful day.
Posted
by
Post not yet marked as solved
1 Replies
495 Views
I am trying to develop an app that enables calligraphers to use their Apple Pencil as a calligraphy pen. The problem I am facing is that I don't know how to customize the strokes drawn by the Apple Pencil using the PencilKit framework. I also tried to use UIKit and handle the touches by Apple Pencil, but I am not sure how to achieve the desired effect. Can anyone guide me to solve this issue?
Posted
by
Post not yet marked as solved
0 Replies
592 Views
I am building a PencilKit app that interprets a canvasView.drawing's size. Drawings below a certain size are interpreted as tap gestures and get cleared in the interpretation process. Effectively, I am doing it like this: func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) { // Check if drawing is empty, // otherwise this would loop guard !canvasView.drawing.bounds.isEmpty else { return } if canvasView.drawing.bounds.width 10 && canvasView.drawing.bounds.height 10 { canvasView.drawing = PKDrawing() handleDetectedTapGesture() } } On my 2020 iPad Pro, this works flawlessly. On other iPads however, I observe a strange behaviour: In a series of drawing little specks on the canvas that are all supposed to be cleared immediately, sometimes the canvas seems to not be cleared, a single speck stays visible. After I draw the next speck somewhere else, the clearing is functional again, which means the canvas was internally emptied, the visible state was just not up-to-date. Finger input on the canvas is deactivated. The canvas tool is set to pencil, width 5.0. Does anybody have an idea what could be the reason for this behaviour?
Posted
by
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
by
Post not yet marked as solved
5 Replies
1.2k Views
For context: I'm working on a score pad app, with the ability to have a column of PKCanvases, so you can just hand write things without them being scribbled into text. As part of my stress testing, I basically filled the grid with ~50 canvases, and it seems that after a certain amount of CanvasViews are added to the score pad, my M1 iPad Pro GPU is unable to keep up. Occasionally, a cell will just entirely render as magenta, and I will have dozens and dozens of Execution of the command buffer was aborted due to an error during execution. Insufficient Memory (IOAF code 8) logs thrown to the console. I honestly don't know where to go from here. I recognize that what I'm doing is almost definitely not what PencilKit was designed for, but I also know the API is relatively young. I can't tell if this is a bug that should be fixed by Apple, or entirely my fault and a sign that I need to redo the entire thing. I've attached a screenshot with the magenta cell, there's also another two cells where the pencil strokes are just thick black squares. I'd appreciate any advice or help, I can't even seem to find any way to detect and recover from this in my code, as trying to search for Metal errors gives me discussions about writing Metal apps, which this isn't. It's a pure SwiftUI app with some PencilKit canvases.
Posted
by
Post not yet marked as solved
1 Replies
553 Views
Hy! I saw your video Inspect, modify, and construct PencilKit drawings. Literally it is amazing I've a question in that we can drawing only by apple pencil but i want to draw by fingers can you tell me how it is possible. I shall be very grateful to you. Thanks
Posted
by
Post not yet marked as solved
0 Replies
398 Views
on iOS, I want to add up undo/redo and a close button. On ipadOS, I only need to add a close button What’s your experience in adding a close button to the ToolPicker? Or at least have the position of the window so I can add an overlapping box (even on floating).
Post not yet marked as solved
0 Replies
507 Views
Hy! Hope you are all doing well. I saw video of Inspecting, Modifying, and Constructing PencilKit Drawings. This video clear my all concepts. But I've one issue I want to draw on numbers like alphabets but there is no numbers file. In this code only uppercase and lowercase drawing file but these files not open. Firstly kindly tell me how to open this file and how to draw on numbers like alphabets. Your solutions are appreciation for me. I am waiting for your solution. Thanks.
Posted
by
Post not yet marked as solved
0 Replies
534 Views
I'm implementing swift UI image view overlaid by canvas view in Pencil kit Because Pencil kit is in UIKit I create another class for creating gesture recognizer Thankfully it work so I can zoom or pan on canvas view and the underling image view is responding But unlikely using Gesture and GestureState in SwiftUI It cause memory issue when I just zoom image it take almost 1GB Here is my code class GestureDelegate: NSObject, ObservableObject, UIGestureRecognizerDelegate { var zoomScale: CGFloat { fixedZoomScale * gestureZoomScale } var fixedZoomScale: CGFloat = 1 @Published var gestureZoomScale: CGFloat = 1 private(set) lazy var pinchGestureRecognizer: UIPinchGestureRecognizer = { let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchImage(_:))) pinchGesture.delegate = self return pinchGesture }() Here is canvas view in UIViewRepresentable There is pan gesture recognizer also but there is no problem with pan gesture struct BlurMaskView: UIViewRepresentable { private let canvas: PKCanvasView private let gestureDelegate: GestureDelegate func makeUIView(context: Context) -> PKCanvasView { canvas.drawingPolicy = .anyInput canvas.tool = tool canvas.backgroundColor = .clear canvas.addGestureRecognizer(gestureDelegate.pinchGestureRecognizer) canvas.addGestureRecognizer(gestureDelegate.panGestureRecognizer) return canvas }
Posted
by
Post not yet marked as solved
0 Replies
483 Views
Hy! Hope you are all doing well. I already ask this question but no one give me any solution. I saw video of Inspecting, Modifying, and Constructing PencilKit Drawings. This video clear my all concepts. But I've one issue I want to draw on numbers like alphabets but there is no numbers file. In this code only uppercase and lowercase drawing file but these files not open. Firstly kindly tell me how to open this file and how to draw on numbers like alphabets. Your solutions are appreciation for me. I am waiting for your solution. Thanks.
Posted
by
Post not yet marked as solved
0 Replies
406 Views
Hi , I was watching https://developer.apple.com/videos/play/wwdc2020/10148/ And I did read the demo project code but I don't understand how Apple know when write "W" in textfield it's "W" in drawing area ? I understand that we separate every char from drawing data , but how it matching with the textfield text ? this part it's confusing me
Posted
by
Post not yet marked as solved
0 Replies
384 Views
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??
Posted
by
Post marked as solved
3 Replies
895 Views
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
Posted
by
Post not yet marked as solved
0 Replies
419 Views
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?
Posted
by
Post not yet marked as solved
2 Replies
517 Views
I'm currently using PencilKit in an app that is forced to run in dark mode (UIUserInterfaceStyle == Dark in Info.plist). But for one UIViewController only, I'm using the light mode for note taking with PencilKit. I change the ToolPicker style by using toolPicker.overrideUserInterfaceStyle = .light and it works great that way. Unfortunately, if I want a custom color and I tap on the ColorPicker icon, it seems to still be in dark mode, because when I choose white, it draws black, and if I choose black, it draws black. This doesn't happen if I tap the black color in the predefined palette (not the color picker). Any idea how to force to color picker to respect the tool picker userInterfaceStyle?
Posted
by
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
Post not yet marked as solved
2 Replies
383 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
by