How to assign value of type 'String?' to type 'UIImage?'

560 Views Asked by At

I am trying to load the image from the response but it is showing "Cannot assign value of type 'String?' to type 'UIImage?'". I have already loaded the labels in tableView but unable to load the image. I have used alamofire for API calls. This is my code. Thanks in advance

 extension ContactVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        arrData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as? ContactCell
        cell?.lblEmpName.text = self.arrData[indexPath.row].name
        cell?.lblEmpDesignation.text = self.arrData[indexPath.row].designation
        cell?.imgEmp.image = self.arrData[indexPath.row].profilePhoto
        return cell!
    } 
1

There are 1 best solutions below

0
On BEST ANSWER

It seems that profilePhoto is a String which may contain the URL for the image. So you must first download that image and then assign it to the image view in your cell:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as? ContactCell
    cell?.lblEmpName.text = self.arrData[indexPath.row].name
    cell?.lblEmpDesignation.text = self.arrData[indexPath.row].designation
    Alamofire.request(self.arrData[indexPath.row].profilePhoto).responseImage { response in
        if let image = response.result.value {
            cell?.imgEmp.image = image
        }
    }
    return cell!
}