Swift 4 AlamofireImage with UITableView Cell

后端 未结 10 3242
梦如初夏
梦如初夏 2021-02-20 12:56

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

相关标签:
10条回答
  • 2021-02-20 13:39

    Of course your image will not show for you. Cause when Alamofire starts downloading your image its process in background thread. But you are working in Main Thread or UI Thread so you need to swap from background to Main Thread it will work for you.

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

    If you think it is difficulty for you to do this i recommend you to use this library called Kingfisher it will handle asynchronous loading images for you that make your cell's table run in smoothly.

    Link: https://github.com/onevcat/Kingfisher

    0 讨论(0)
  • 2021-02-20 13:45

    Swift-5 Supported Code That Work Perfectly

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tblView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        cell.lblName.text = arrData[indexPath.row]["name"] as? String
    
      let imageUrl = arrData[indexPath.row]["imageUrl"] as? String
    
        Alamofire.request(imageUrl!, method: .get).response { response in
            guard let image = UIImage(data:response.data!) else {
                // Handle error
                return
            }
            let imageData = image.jpegData(compressionQuality: 1.0)
            cell.imgCourses.image = UIImage(data : imageData!)
        }
    
        return cell
    } 
    
    0 讨论(0)
  • 2021-02-20 13:46

    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.

    0 讨论(0)
  • 2021-02-20 13:46

    You need to run this code in the main thread.

    DispatchQueue.main.async {
        cell.imageView?.image = image
    }
    
    0 讨论(0)
  • 2021-02-20 13:48

    your code is correct. i think the transport security block your image over http . add this into your info.plist file

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>example.com</key>         //put your domain name here
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>
    

    and you can also use the alamofire's image library. just add pod alamofireImage.

    0 讨论(0)
  • 2021-02-20 13:53

    First you have to check , have you got the proper response if you got the reasponse than write the code like

    Alamofire.request(imageUrl!, method: .get).response { response in
    
                               if let image = UIImage(data: response.data!) {
                              cell. imageView?.image = image
                         }
          }
    
    0 讨论(0)
提交回复
热议问题