How to save a imageview as binary data in Swift 5

I am trying to save the imageview to core data as a binary data. In my function I have already saved a string and it works perfectly. However when I try to do the exact same thing with (theTitle2) it does not work. I also have class cdhandler in my question below which is what is saved in app deleagate.I think what I have to do is somehow convert the image view to a uiimage then save it.

                      func enterData() {

let appDeldeaget = UIApplication.shared.delegate as! AppDelegate
let context = appDeldeaget.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: “User”, in: context)
let theTitle = NSManagedObject(entity: entity!, insertInto: context)
theTitle.setValue(enter.text, forKey: “userName”)
let theTitle2 = NSManagedObject(entity: entity!, insertInto: context)
theTitle2.setValue(imageV.image, forKey: “pit”)

do {

try context.save()
itemName.append(theTitle)


       }
        catch {  }
   //


      }

APP DELEGATE

                class cdHandler: NSObject {

class func saveObject(userName: String) → Bool {
let context = getContext()
let entity = NSEntityDescription.entity(forEntityName: “User”, in: context)

let managedObject = NSManagedObject(entity: entity!, insertInto: context)

managedObject.setValue(userName, forKey: "userName")
managedObject.setValue(userName, forKey: "pit")

do {
    try context.save()
    return true

} catch {
    return false

}

}
}

@timswift Do you still have issues with this?

No this issue has been resolved thanks for looking at it.

This topic was automatically closed after 166 days. New replies are no longer allowed.