Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swift Image conversion from string data type to Image

I am trying to display the image from table view cell to another controller . I am using table view cell to display the image , title and other properties . When the use click the table cell , I want to redirect the user in another controller and display those properties . I am trying to access the defined methods but I am getting following error Cannot assign value of type ‘Data?’ to type ‘UIImage?’ in didSelectRow function . The data is coming from API. Here is the Json struct .

import Foundation

struct Movie: Decodable {
    let originalTitle: String
    let overview: String
    let posterPath: String
    
    enum CodingKeys: String, CodingKey {
        case originalTitle = "original_title"
        case overview
        case posterPath = "poster_path"
    }
    
}

Here is the methods I defined to retrieve those values .

   func getTitle(by row: Int) -> String? {
        return movies[row].originalTitle
    }
    
    func getOverview(by row: Int) -> String? {
        return movies[row].overview
    }
    
    func getImageData(by row: Int) -> Data? {
        return cache[row]
    }

Here is the Cell properties where I converted data to image .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class MovieViewCell: UITableViewCell {
    
    static let identifier = "MovieViewCell"
    

    @IBOutlet weak var movieImage: UIImageView!
    @IBOutlet weak var movieTitle: UILabel!
    @IBOutlet weak var movieOverview: UILabel!
    
    func configureCell(title: String?, overview: String?, data: Data?) {
        
        movieTitle.text = title
        movieOverview.text = overview
        
        if let imageData = data{
            movieImage.image = UIImage(data: imageData)
        }
    }
    
}

Here is the didSelectRow function .

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dc = storyboard?.instantiateViewController(withIdentifier: "MovieDeatilsViewController") as! MovieDeatilsViewController
        
        let row = indexPath.row
       dc.titlemovie = presenter.getTitle(by: row) ?? ""
       dc.overview = presenter.getOverview(by: row) ?? ""
      dc.imagemovie = presenter.getImageData(by: row)
      
        self.navigationController?.pushViewController(dc, animated: true)
   }
    
    
}

Here is the details view controller code where I want to display the details .

class MovieDeatilsViewController: UIViewController {
    
    
    @IBOutlet weak var movieImage: UIImageView!
    
    @IBOutlet weak var movieTitle: UILabel!
    
    @IBOutlet weak var movieOverview: UILabel!
    
    
    var titlemovie = ""
    var overview = ""
    var imagemovie :UIImage?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        movieTitle.text = titlemovie
        movieOverview.text = overview
        movieImage.image = imagemovie
        
    
        
    }

}

Here is the screenshot of the error .

error

>Solution :

If you look at the method signature for getImageData:, you’ll see that it returns Data?. Conversely, your imagemovie property is expecting a UIImage?. See where the issue is?

func getImageData(by row: Int) -> Data? {

var imagemovie :UIImage?

Instead of assigning a value of type Data? to your imagemovie property, you first must convert the Data? to a UIImage? like so:

dc.imagemovie = UIImage(data: presenter.getImageData(by: row)!)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading