Downloading UIImage via AlamofireImage? [duplicate]

You can use the below function for downloading the image:

func getImage(_ url:String,handler: @escaping (UIImage?)->Void) {
        print(url)
        Alamofire.request(url, method: .get).responseImage { response in
            if let data = response.result.value {
                handler(data)
            } else {
                handler(nil)
            }
        }
    }

Uses

getImage("http://") { (image) in
    if image != nil {
        print(image)
    }
}

Or

If you want to set the image on UIImageView use extension of AlamofireImage.

if let imageURL = URL(string: "http://"), let placeholder = UIImage(named: "default") {
     imageView.af_setImage(withURL: imageURL, placeholderImage: placeholder) //set image automatically when download compelete.
}

Leave a Comment