Search code examples
iosswiftuitableviewalamofirealamofireimage

Swift 4 AlamofireImage with UITableView Cell


I am using AlamofireImages to try to download an image from a server and display it in my table view cell, this is what I got, but my images does not appear, just the textLabel and detailTextLabel

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "trendingCell", for: indexPath)

        print(self.array[indexPath.row])

        cell.textLabel?.text = (self.array[indexPath.row]["title"] as! String)

        cell.detailTextLabel?.text = (self.array[indexPath.row]["username"] as! String)

        Alamofire.request("http://xxxxxxx.com/uploads/" + (self.array[indexPath.row]["cover"] as! String)).responseImage { response in
            if let image = response.result.value {
                cell.imageView?.image = image
            }
        }

        return cell
    }

Solution

  • Try this:

     Alamofire.request(imageUrl!, method: .get).response { response in
                        guard let image = UIImage(data:response.data!) else {
                            // Handle error
                            return
                        }
                        let imageData = UIImageJPEGRepresentation(image,1.0)
                        cell.myImage.image = UIImage(data : imageData!)
                    }
    

    This works fine for me.. Hope This will helpful.