How to store an image in core data

前端 未结 7 1197
你的背包
你的背包 2020-11-27 09:59

I\'m new to iOS. I\'ve been trying to make an application that will store an image captured from the camera into CoreData. I now know how to store data like

7条回答
  •  抹茶落季
    2020-11-27 10:34

    For Swift 5 and Swift 4.2

    1. convert UIImage to Data:

      let imageData = image.jpegData(compressionQuality: 1.0)
      
    2. save to CoreData:

      let object = MyEntity(context: managedContext)   //create object of type MyEntity
      object.image = imageData                         //add image to object
      
      do {
          try managedContext.save()                   //save object to CoreData
      } catch let error as NSError {
          print("\(error), \(error.userInfo)")
      }
      

提交回复
热议问题