Saving image to Core Data?

时间:2017-08-05 11:45:06

标签: ios swift core-data alamofireimage

I want to parse these small avatar images to core data, I have this function using AlamofireImage.

However, on the setting of the result to the core data property, I get this error.

Cannot assign value of type 'Data?' to type 'NSData?'

Here is my code:

self.managedObjectContext = self.coreDataManager.managedObjectContext
let contactAvatar = UIImagePNGRepresentation(image);
contact.avatar = contactAvatar
do {
    try self.managedObjectContext?.save()
} catch {
    fatalError("Failure to save context: \(error)")
}

My Core data entry is Binary Data. I thought I should be able to save to this format correctly?

1 个答案:

答案 0 :(得分:1)

There are two options:

  • Declare the attribute avatar as Data in the model (preferable)
  • Bridge cast contactAvatar as NSData