1

I have a PKCanvasView in my IOS app. When the user puts in a drawing, it saves the strokes as a PKDrawing. This drawing could be accessed on many different devices with different screen sizes. However, when a drawing that is created on a larger device, eg, an iPad, is viewed on a smaller device, eg, an iPhone, the drawing is cut off from the bottom right. Additionally, when a drawing created on a smaller device is viewed on the bigger device, it does not fill the space but rather is confined to the top right corner.

My Question: Is there any way to scale a PKDrawing up or down to fit the PKCanvasView's frame?

JustMakeStuff
  • 419
  • 3
  • 19
  • There is no way you can move PKDrawing from one device to another. You need to save it as an image and display in other devices. – Shreeram Bhat Dec 13 '21 at 07:33
  • I have successfully moved the `PKDrawing` from one device to the other, the only problem is that it does not display properly on some smaller devices as I said in my question. I need to store the drawing as the strokes because the drawing needs to be able to be edited on the other devices. – JustMakeStuff Dec 13 '21 at 16:45

1 Answers1

4

I fixed it by using the transform function to scale the drawing up or down to fit the screen size. I made an extension:

extension PKDrawing {
    mutating func scale(in frame: CGRect) {
        var scaleFactor:CGFloat = 0
        
        if self.bounds.width != frame.width {
            scaleFactor = frame.width / self.bounds.width
        } else if self.bounds.height != frame.height {
            scaleFactor = frame.height / self.bounds.height
        }
        
        let trasform = CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)
        
        self.transform(using: trasform)
    }
}
JustMakeStuff
  • 419
  • 3
  • 19