Downloading Uiimage via Alamofireimage

Downloading UIImage via AlamofireImage?

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.
}

Image downloading with progress using AlamofireImage?

There is not any way at the moment to use AlamofireImage alongside the UIImageView extension to receive progress updates when downloading an image. The reason it wasn't added initially was that it didn't seem like the majority of users would need such a feature. I would love to discuss more to see if this is a feature that we'd actually like to add to AlamofireImage in a future release.

Would you be willing to open up an issue walking through your use case? I would just like to know exactly how you would expect it to work and why you actually need the progress reporting.

AlamofireImage: How to downdload images with POST request

You can use af_setImage method from AlamofireImage extension of UIImageView and pass any URLRequestConvertible parameter. For example, create URLRequest instance with Alamofire initializer:

let urlPath = "https://host_name/project_name/GetImage"
if var imageRequest = try? URLRequest(url: urlPath, method: .post) {
imageRequest.addValue("token", forHTTPHeaderField: "token_field")
imageView.af_setImage(withURLRequest: imageRequest)
}

Resize Download Image with AlamofireImage

You can use filters:

let url = URL(string: ...)!
let placeholder = UIImage(named: "app_default-resize")
let filter = AspectScaledToFillSizeFilter(size: imageView.frame.size)
imageView.af.setImage(withURL: url, placeholderImage: placeholder, filter: filter)

See https://github.com/Alamofire/AlamofireImage#image-filters-1.

For Swift 2 version, see previous revision of this answer.

Show image in UIImageview by using alamofireimage

You did not set the image as your imageView's image. Here is your code, fixed:

Alamofire.request("https://httpbin.org/image/png").responseImage { response in
if let image = response.result.value {
self.imageView.image = image
}
}

Alamofire and downloading images from server

SOLVED: The problem was with Alamofire request, I was able to resolve the issue with using AlamofireImage framework.

download images using alamofire - iOS

What you are asking is really bad practice in mobile app. Just a case, for example, you made a request and got like 20 items in an array, and in order to put all UIImages in your models, you have to make 20 more requests, also you even don't know if your users will eventually use (view) those icons or no.

Instead, you could fetch the images when the cell (I guess, you will be displaying those icons in a cell) is displayed, for this purpose you could use libs like SDWebImage(objective c) or Kingfisher(swift) which have extensions for UIImageView to make it simple to fetch and display image on. Those libs can also cache the downloaded image.

Also, another suggestion for object mapping. Currently, you are mapping json to your model manually. There are a lot of good libs to handle that for you, which could automate your object mapping process, for example - ObjectMapper

Hope, this was helpful. Good Luck!



Related Topics



Leave a reply



Submit