Save and retrieve the CGPath in CoreData iOS swift

Problem:

I want to save the CGpath to the Coredata and retrieve the path and redraw it.


Solution:

Save to coredata:

let objDict = NSMutableDictionary()
              objDict.setValue(NSKeyedArchiver.archivedData(withRootObject: Color), forKey: "color")
  let bezierPath = UIBezierPath(cgPath: tool.penPath)
objDict.setValue(bezierPath, forKey: "Path")
  objDict.setValue(NSNumber(value: Double(tool.lineWidth)), forKey: "width")
            

From coredata and redraw:


let path = k.value(forKey: "Path")
let shapeView: CAShapeLayer = CAShapeLayer.init()
 shapeView.path = (path as! UIBezierPath).cgPath
  shapeView.strokeColor = UIColor.white.cgColor
 shapeView.lineWidth = CGFloat(k.value(forKey: "width") as! Double)                    self.ciew.layer.addSublayer(shapeView)

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift