0

I did a set up of CoreData with entity "users" and attribute "username" (when I type something in uitextfield it saves into core data and I got readings from core data into tableview cell). That is working like a charm. But I also want to display chosen picture from imagepickercontroller and that is also working fine when I choose picture but when I click save I think I got it saved. I don't know if it is saved because I eliminated all error so I got no errors, but it won't read that picture into tableview cell. I put new attribute "image" with type binary data into core data file but code does not work.

This is code for saving into core data

let newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context)

newUser.setValue(nameField.text, forKey: "username")

let picture = UIImageJPEGRepresentation(photoImageView.image!, 1); 
newUser.setValue(picture, forKey: "image") 

And this is code where tableview reads data from core data

cell.imageView!.image = person.valueForKey("image") as? UIImage

I also tried with

[UIImage, imageWithData:self.myEvent.picture];

but Swift 2.0 don't have that syntax imageWithData

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Igy
  • 129
  • 1
  • 16

2 Answers2

0

person.valueForKey("image") as? UIImage won't work.

Use UIImage(data:) to convert the retrieved NSData to UIImage.

MirekE
  • 11,515
  • 5
  • 35
  • 28
  • let picture object is in first vc , tableview is in second, so how in second vc get that let picture object? – Igy Oct 06 '15 at 15:37
  • tried with this in second vc but it does not work let image : UIImage = UIImage(data: person.valueForKey("image") as? UIImage) – Igy Oct 06 '15 at 15:40
  • It should not be `as? UIImage`, you saved it as `NSData` with the `UIImageJPEGRepresentation`. As far as passing data to another view controller, you could use `prepareForSegue`. – MirekE Oct 06 '15 at 15:45
  • this code is for retrieving data from core data cell.textLabel!.text = person.valueForKey("username") as? String, how can i use the same principle for uiimage? – Igy Oct 06 '15 at 15:47
  • You can't because you did not save your data as UIImage. You saved it as NSData with jpeg representation. Retrieve it as NSData and convert to UIImage using UIImage(data:) – MirekE Oct 06 '15 at 16:07
0

for retrieving image from core data i used this and it is finally working.

let image = person.valueForKey("image") as? NSData
    cell.imageView!.image = UIImage(data: image!)
Igy
  • 129
  • 1
  • 16