How to load image in swift using Alamofire

后端 未结 9 905
醉话见心
醉话见心 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 14:00

    You can download image by using responseData(queue:completionHandler:)

    SWIFT 4

    func downloadImages(imageURL: String) {
    
        Alamofire.request(imageURL, method: .get)
            .validate()
            .responseData(completionHandler: { (responseData) in
                self.yourImageVIew.image = UIImage(data: responseData.data!) 
                DispatchQueue.main.async {
                    // Refresh you views
                }
            })
    }
    
    0 讨论(0)
  • 2020-12-13 14:01

    In swift 5 & Alamofire 5,You can use like bellow..

      AF.request( "https://robohash.org/123.png",method: .get).response{ response in
    
       switch response.result {
        case .success(let responseData):
            self.myImageView.image = UIImage(data: responseData!, scale:1)
    
        case .failure(let error):
            print("error--->",error)
        }
    }
    
    0 讨论(0)
  • 2020-12-13 14:01

    If you want to use AFNetworking's UImageView framework, you have to do #import "UIImageView+AFNetworking.h" in your bridge-header file.

    AFNetworking has a shared cache, so you won’t need to manually cache or cancel your requests. The setImageWithURL from AFNetworking can easily convert a URL to an image in ImageView.

    Sample code using UIImage+AFNetworking:

    cell.imageView.setImageWithURL(NSURL(string:imageSource), placeholderImage: UIImage(named:"placeholder"))
    

    If you are using Alamofire, you have to manually convert NSData to UIImage. For Alamofire, you can create a NSCache() object to cache your images. The performance of both library should be similar. You can use Alamofire for your server API calls and then use AFNetworking to display images asynchronously.

    Sample code using Alamofire for image cache:

    let imageCache = NSCache()
    
     if let image = self.imageCache.objectForKey(imageURL) as? UIImage {
        cell.imageView.image = image
      } else {
        // 3
        cell.imageView.image = nil
    
        // 4
        cell.request = Alamofire.request(.GET, imageURL).validate(contentType: ["image/*"]).responseImage() {
          (request, _, image, error) in
          if error == nil && image != nil {
            // 5
            self.imageCache.setObject(image!, forKey: request.URLString)
    
            // 6
            if request.URLString == cell.request?.request.URLString {
              cell.imageView.image = image
            }
          } else {
            /*
            If the cell went off-screen before the image was downloaded, we cancel it and
            an NSURLErrorDomain (-999: cancelled) is returned. This is a normal behavior.
            */
          }
        }
      } 
    

    You can read this tutorial for more detail about using Alamofire.

    0 讨论(0)
提交回复
热议问题