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.
我使用实体“users”和属性“username”设置了CoreData(当我在uitextfield中键入内容时,它会保存到核心数据中,并且我从核心数据读取到tableview单元格中)。这就像一个魅力。但是我也希望显示来自imagepickercontroller的所选图片,当我选择图片时也能正常工作但是当我点击保存时我想我已经保存了它。我不知道它是否被保存,因为我消除了所有错误,所以我没有错误,但它不会将该图片读入tableview单元格。我将带有二进制数据类型的新属性“image”放入核心数据文件中但代码不起作用。
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
这是tableview从核心数据中读取数据的代码
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
但Swift 2.0没有语法imageWithData
2 个解决方案
#1
0
person.valueForKey("image") as? UIImage
won't work.
person.valueForKey(“image”)为? UIImage不起作用。
Use UIImage(data:)
to convert the retrieved NSData to UIImage.
使用UIImage(data :)将检索到的NSData转换为UIImage。
#2
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!)
#1
0
person.valueForKey("image") as? UIImage
won't work.
person.valueForKey(“image”)为? UIImage不起作用。
Use UIImage(data:)
to convert the retrieved NSData to UIImage.
使用UIImage(data :)将检索到的NSData转换为UIImage。
#2
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!)