How to load image in swift using Alamofire

后端 未结 9 1037
醉话见心
醉话见心 2020-12-13 13:16

I have a collection view controller, which load image Async by URL. (Something like Instegram) I am looking for the best way to implement the part of the loading image. plea

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 13:35

    Actually there are multiple ways to load your image from URL using Alamofire. and this is a way that i've developed and i'm using it already in one of my Swift projects. This way i'm loading the images Asyncrounsly and display them into UITableView and from performance wise it's fine and there is no blocking while scrolling UITableView because it's care about cashing the images loaded.

    First you have to create a GET request using Alamofire to download the image, and you have to use Request+AlamofireImage from AlamofireImage Library so you have to

    import AlamofireImage
    

    And then in

        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    

    }

    you have to call your image download request like this:

             //Download image
            // We should perform this in a background thread
            Alamofire.request(.GET, deshItem.dish_imageUrl!)
                .responseImage { response in
                    debugPrint(response)
    
                    print(response.request)
                    print(response.response)
                    debugPrint(response.result)
    
                    if let image = response.result.value {
                        print("image downloaded: \(image)")
    
                        // Store the commit date in to our cache
                        self.ImageCache[dishName!] = image
    
                        // Update the cell
                        dispatch_async(dispatch_get_main_queue(), {
                            if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                                    let dishImageView:UIImageView = cellToUpdate.viewWithTag(104) as! UIImageView
                                    dishImageView.image = image
    
                            }
                        })
    
                    }
            }
    

    For Image Caching i've created a dictionary that will contain image for every cell or item(String), So this way of caching images will avoid the locking that appear while scrolling.

    Complete Sample code:

    import AlamofireImage
    
    class MainFeedTableViewController: UITableViewController, FloatRatingViewDelegate {
    
    var ImageCache = [String:UIImage]()
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("MainFeedCellID", forIndexPath: indexPath)
    
        // Configure the cell...
    
        let dishName = deshItem.dish_name
    
        //This is a workaround to cash the image and improve the performance while user scrolling UITableView.
        // If this image is already cached, don't re-download
        if let dishImage = ImageCache[dishName!] {
                    let dishImageView:UIImageView = cell.viewWithTag(104) as! UIImageView
                    dishImageView.image = dishImage
        }
        else {
            //Download image
            // We should perform this in a background thread
            Alamofire.request(.GET, deshItem.dish_imageUrl!)
                .responseImage { response in
                    debugPrint(response)
    
                    print(response.request)
                    print(response.response)
                    debugPrint(response.result)
    
                    if let image = response.result.value {
                        print("image downloaded: \(image)")
    
                        // Store the commit date in to our cache
                        self.ImageCache[dishName!] = image
    
                        // Update the cell
                        dispatch_async(dispatch_get_main_queue(), {
                            if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                                    let dishImageView:UIImageView = cellToUpdate.viewWithTag(104) as! UIImageView
                                    dishImageView.image = image
    
                            }
                        })
    
                    }
            }
    
        }
    
    
    
        return cell
    }
    

提交回复
热议问题