Display binary data on image view

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!){

          }}

}


I want to do the same thing only displaying the binary data as a uiimage in a collection view cell.

Hi @timswift,
What is your binary data - an Image? If it is, you have a couple of options where you can create an image from the binary data.

You can see the various options here Apple Developer Documentation

it all depends on how you have saved it, if it is simply the binary representation of the entire file then there would be a different way to load it.

the one that is of interest to you is
UIImage(data: <data>)

cheers,

Jayant

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.

cheers,

Jayant

@jayantvarma
Added a photo. This is my coreData.

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.

cheers,

Jayant

@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

cheers,

Jayant

@jayantvarma I am confused on what to put in let imageData. Thanks for your help.

@jayantvarma this is what I put in let imageData = users[indexPath.row].pic.

Hi @timswift,
Yes that is how it should be, but ensure that the binaryData is Data .

if let imageData = users[indexPath.row].pic as? Data {
 //
}

Have you seen the Videos on working with Core Data? It will address exactly what you are after.

https://videos.raywenderlich.com/courses/98-beginning-core-data/lessons/6

cheers,

Jayant

This topic was automatically closed after 166 days. New replies are no longer allowed.