Save image preloaded into Xcode project using pngdata

In my swift code the goal is to save a image which is already in the Xcode project to core data. Right now I am running into issues unwrapping a optional value on the let entity declaration. That is causing a compile issue.

override func viewDidLoad() {
    let vex = UIImage(named: "jessica.jpeg")!.pngData()
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let managedContext = appDelegate.persistentContainer.viewContext
    let entity = NSEntityDescription.entity(forEntityName: "Item", in: managedContext)!
    let item = NSManagedObject(entity: entity, insertInto: managedContext)
    if let data = vex{
        item.setValue(data, forKey: "image")

While it’s definitely possible to store large binary blobs into Core Data, it must be said that it’s not the purpose it was meant for. Core Data was meant to store textual data that could be retrieved as a model, searchable (indexable) and synchronizable.

Perhaps storing to the Documents (Files App) would be a better solution?

Or if you want it accessible over several devices, maybe S3?

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