0

I want to save the UIImage that I get from the UIImagePickerCollector into CoreData attribute "img" that is Binary Data type. I just try to save using following code.

let sampleimage = profileimg?.image
    var dataImage: Data? = nil
    if let aSampleimage = sampleimage {
        dataImage = aSampleimage.jpegData(compressionQuality: 0.0)
    }

    let imag = Data()

    let newUser = NSEntityDescription.insertNewObject(forEntityName: "Person", into: Constant.context!)

    newUser.setValue(self.txtName!.text, forKey: Constant.CDName)
    newUser.setValue(self.txtEmail!.text, forKey: Constant.CDEmail)
    newUser.setValue(self.txtPassword!.text, forKey: Constant.CDPassword)
    newUser.setValue(yourDate, forKey: Constant.CDDob)
    newUser.setValue(imag, forKey: Constant.CDImage)

When I try to run the it not showing any error. But I don't know image is save in CoreData OR not. How can I check it?

And when I try to assign the image into my table view cell it's show me following error. Please prove proper solution.

Krunal Nagvadia
  • 1,083
  • 2
  • 12
  • 33

2 Answers2

0

You are getting NSData to UIImage conversion error. Try using UIImage(data:imageData,scale:1.0) presuming the image's scale is 1.

For you code it could be something like :

if let imgData = obj.img {
    cell.listImg.image = UIImage(data:imgData, scale:1.0) //presuming the image's scale is 1
}

Other variation could be

if let imgData = obj.img {
    cell.listImg.image = UIImage(data: imgData)
}

Saving image, In the statement newUser.setValue(imag, forKey: Constant.CDImage), you are using imag, however actual image is contained in dataImage. Please correct it

newUser.setValue(imag, forKey: Constant.CDImage)

to

newUser.setValue(dataImage, forKey: Constant.CDImage)

Also for the shared code snipped, I don't see any use of let imag = Data(), you can remove it if its not being used beyond the part of code you shared.

Rizwan
  • 3,324
  • 3
  • 17
  • 38
  • `Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value` This error is showing. I think image is not save into CoreData correctly. – Krunal Nagvadia Oct 26 '18 at 06:28
  • use Optional Chaining to avoid this error. Please check updated answer. – Rizwan Oct 26 '18 at 06:29
  • Where is optional chaining in updated answer ? Do you understand the difference between optional unwrapping and optional chaining ? – Sharad Chauhan Oct 26 '18 at 06:42
  • My Bad I mean optional unwrapping with if let. Thanks for correcting. – Rizwan Oct 26 '18 at 06:44
  • ok. the error is gone now but the image is still not showing. Can you please check and let me know that, Is that a correct way to save image into CoreData? – Krunal Nagvadia Oct 26 '18 at 06:45
0

If you want to save the image, you can set the field as Binary data and you have to convert and save to the core data

UIImage to Data

if let img = image {
   let data = UIImagePNGRepresentation(img!)
}

and when you fetch

Data to UIImage

if let img_data = data {
   let image = UIImage(data:img_data)
}

so like this you have to create the object of coredata entity and then you have to create field as binary data and save the data of UIImage and when you want to fetch then again need to convert from data to image and assign to your UIImageview.

Ronak Adeshara
  • 321
  • 4
  • 13