简体   繁体   中英

How to save Image in Core Data

I am trying to save an image to core data . I get an image from camera or photo library an store it to global UIImage variable . How can I insert this image to newPerson.image

let newPerson = NSEntityDescription.insertNewObject(forEntityName: "Person", into: manageObjectContext) as! Person
newPerson.age = age
newPerson.city = cityInput.text
newPerson.image = ????

I am using swift 4.

Convert UIImage to NSData and save.

To save: let data = UIImagePNGRepresentation(image)

To load: let image = UIImage(data: data)

Convert the image into Data form. Then save it as Binary data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM