TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Not able to show image on the UI

Forums > Swift

currently I'm in day 43 where I have complete upto the Connecting up the people and done exactly same way but not able to show image on UI any Idea

https://drive.google.com/file/d/1GsTuhpJLzrd3ufLRXmB6Uxj16jDD1cJm/view?usp=sharing

code:

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Person", for: indexPath) as? PersonCell else {
            fatalError("didn't get peson cell")
        }

        cell.name.text = personList[indexPath.item].name

        let path = getDocumentDirectory().appendingPathComponent(personList[indexPath.item].image)

        cell.imageView.image = UIImage(contentsOfFile: path.path)

        cell.imageView.layer.borderColor = UIColor(white: 0, alpha: 0.3).cgColor
        cell.layer.borderColor = UIColor(white: 0, alpha: 0.3).cgColor
        cell.imageView.layer.borderWidth = 1
        cell.layer.borderWidth = 1
        cell.imageView.layer.cornerRadius = 3
        cell.layer.cornerRadius = 7

        return cell
    }

3      

Hi there!

Check how you save your image in imagePickerController...

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard let image = info[.editedImage] as? UIImage else { return }

    let imageName = UUID().uuidString
    let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)

    if let jpegData = image.jpegData(compressionQuality: 0.8) {
      try? jpegData.write(to: imagePath)
    }

    let person = Person(name: "Unknown", image: imageName)
    people.append(person)
    collectionView.reloadData()

    dismiss(animated: true)

  }

maybe you need to reload data in collection view, or something is missing...

3      

no i have done that to

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.editedImage] as? UIImage else { return }

        let imageName  = UUID().uuidString
        let imagePath = getDocumentDirectory().appendingPathExtension(imageName)

        if let jpegData = image.jpegData(compressionQuality: 0.8) {
            try? jpegData.write(to: imagePath)
        }

        let person = Person(name: "Unknown", image: imageName)

        personList.append(person)
        collectionView.reloadData()

        dismiss(animated: true)
    }

you can check in give link that the personList list is updating but image is not showing

but thanks for help @ygeras

3      

Try to clean build folder, by shift + commad + K. Or provide full code for ViewController maybe there is something else affects that.

3      

also clean build folder but same result

full code:

//
//  ViewController.swift
//  project10
//
//  Created by Divyang Parmar on 24/08/23.
//

import UIKit

class ViewController: UICollectionViewController, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
    var personList = [Person]()

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNewPerson))
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return personList.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Person", for: indexPath) as? PersonCell else {
            fatalError("didn't get person cell")
        }

        cell.name.text = personList[indexPath.item].name

        let path = getDocumentDirectory().appendingPathComponent(personList[indexPath.item].image)

        cell.imageView.image = UIImage(contentsOfFile: path.path)

        cell.imageView.layer.borderColor = UIColor(white: 0, alpha: 0.3).cgColor
        cell.layer.borderColor = UIColor(white: 0, alpha: 0.3).cgColor
        cell.imageView.layer.borderWidth = 1
        cell.layer.borderWidth = 1
        cell.imageView.layer.cornerRadius = 3
        cell.layer.cornerRadius = 7

        return cell
    }

    @objc func addNewPerson() {
        let picker = UIImagePickerController()
        picker.allowsEditing = true
        picker.delegate = self
        present(picker, animated: true)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.editedImage] as? UIImage else { return }

        let imageName  = UUID().uuidString
        let imagePath = getDocumentDirectory().appendingPathExtension(imageName)

        if let jpegData = image.jpegData(compressionQuality: 0.8) {
            try? jpegData.write(to: imagePath)
        }

        personList.append(Person(name: "UnKnown", image: imageName))
        collectionView.reloadData()

        dismiss(animated: true)
    }

    func getDocumentDirectory() -> URL {
        let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return path[0]
    }
}

3      

Change that line in your imagePickerController func

 let imagePath = getDocumentDirectory().appendingPathExtension(imageName)

to

 let imagePath = getDocumentDirectory().appendingPathComponent(imageName)

it is component not extension.

4      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.