Save uiimageview to coredata as binary data swift (5)

I am trying to save a imageview as a image to binary data in core data. My code is not working. It has a compile error. In View controller it is not regisitering cdHandler. All i want to do is save the the imaveview as binary data in a core data model.I have 2 classes a app delegate and a view controller.

CLASS VIEW CONTROLLER

import UIKit
import CoreData

class ViewController: UIViewController {
var canVasView = UIImageView()
@objc func hhh() {
let photo = self.canVasView.image
let data = photo!.pngData()

if cdHandler.saveObject(pic:  data!){
}

}
}

APP DELEGATE

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

 lazy var persistentContainer: NSPersistentContainer = {
     /*
      The persistent container for the application. This implementation
      creates and returns a container, having loaded the store for the
      application to it. This property is optional since there are legitimate
      error conditions that could cause the creation of the store to fail.
      */
     let container = NSPersistentContainer(name: "Model")
     container.loadPersistentStores(completionHandler: { (storeDescription, error) in
         if let error = error as NSError? {
             // Replace this implementation with code to handle the error appropriately.
             // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

        /*
         Typical reasons for an error here include:
         * The parent directory does not exist, cannot be created, or disallows writing.
         * The persistent store is not accessible, due to permissions or data protection when the device is locked.
         * The device is out of space.
         * The store could not be migrated to the current model version.
         Check the error message to determine what the actual problem was.
         */
        fatalError("Unresolved error \(error), \(error.userInfo)")
    }
})
return container

}()
class cdHandler: NSObject {

private class func getContext() -> NSManagedObjectContext {
    let appdeleagetzz = UIApplication.shared.delegate as! AppDelegate
    return appdeleagetzz.persistentContainer.viewContext
}
class func saveObject(pic: Data, userName: String) -> Bool {
    let context = getContext()
    let entity = NSEntityDescription.entity(forEntityName: "User", in: context)
    let managedObject = NSManagedObject(entity: entity!, insertInto: context)
    managedObject.setValue(pic, forKey:"pic")
    managedObject.setValue(userName, forKey:"userName")
    do {
        try context.save()
        return true

    } catch {
        return false

    }
}
class func deletObject(user: User) -> Bool {
    let context = getContext()
    context.delete(user)

    do {
        try context.save()
        return true
    } catch {
        return false

    }
}

class func fetchObject() -> [User]? {

    do {
        let context = getContext()
        return try context.fetch(User.fetchRequest())
    } catch {
        return [User]()
    }
}

}
}

KYn5H

@timswift Do you still have issues with this?

Yes however I have made some progress. This is a more updated question. How to save layered uiimageview to core data (SWIFT5). But I am still having the problem.

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