I am trying to to call binary data from core data and display in a image view in a collection view cell. This code below works on a string being called from core data and it being displayed in a label.
TRYING TO DISPLAY IMAGE
class whyCollectionViewCell: UICollectionViewCell {
@IBOutlet var general: UILabel!
@IBOutlet var blockbuster: UIImageView!
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
cell.general.text = users[indexPath.row].userName
COREDATAVC
import UIKit
import CoreData
class cdHandler: NSObject {
private class func getContext() -> NSManagedObjectContext {
let appdeleagetzz = UIApplication.shared.delegate as! AppDelegate
return appdeleagetzz.persistentContainer.viewContext
}
class func saveObject(userName: String) -> Bool {
let context = getContext()
let entity = NSEntityDescription.entity(forEntityName: "User", in: context)
let managedObject = NSManagedObject(entity: entity!, insertInto: context)
managedObject.setValue(userName, forKey: "userName")
do {
try context.save()
return true
} catch {
return false
}
}
class func fetchObject() -> [User]? {
do {
let context = getContext()
return try context.fetch(User.fetchRequest())
} catch {
return [User]()
}
}}
ENTER PHOTO
import UIKit
class editVCViewController: UIViewController {
@IBOutlet var imageV: UIImageView!
@IBOutlet var theView: UIView!
@IBOutlet var mattBarnes: UITextField!
@IBAction func magicJohnson(_ sender: Any) {
let photo = self.imageV.image
let data = UIImagePNGRepresentation(photo!)
if cdHandler.saveObject(pic: data!){
}}
}
This is what I put cell.blockbuster.image = UIImage( (users[indexPath.row].pic)!). I am getting a runtime error of fatal unwrapping optional. Any suggestions? Thanks.
Hi @timswift,
getting fatal errors is not a good thing. In my code, I generally try to use ? instead of ! and if I can avoid that, I try and catch the errors.
That only saves from the crashes (which is what Apple likes, they want developers to avoid the apps from crashing - not a good experience).
With the issue at hand. What is the type for .pic? How was it stored into the data? In what format and how? so can you share that along with how are you populating the users object array.
Hi @timswift,
thanks but that is not entirely helpful. How are you populating the data? Because there is a difference between using the contentsOf image file and data from UIImage.
@jayantvarma I have updated my question with 4 parts of the code. The picture of my coreData. The code of my core data, the code of how I enter the image into core data, and where I am trying to call core data to get the binary data to be display as a uiimage.
Hi @timswift,
thanks for that. You have just confirmed what I was asking you since the first question. You are saving the image as PNG but then trying to load it as UIImage, but it is not in UIImage format and hence fails.
UIImagePNGRepresentation or UIImageJPGRepresentation create a binary structure of PNG or JPG that if you save directly to the local storage with a .png or .jpg extension (respectively) that is how it will be saved.
All you need to change is the way you are loading and assigning the image from CoreData to the UI. It is similar to if you were trying to load it from a directory.
//let imageData = Binary data from CoreData
let coredataLoadedimage = UIImage(data: imageData)
blockbuster.image = coredataLoadedImage