How to Load Image in Swift Using Alamofire

Loading Images from URLRequest using Alamofire responseImage IOS swift

Good day. I have already found the solution for this. You would need to reset the value of the imageView to nil at cellForItemAt then assign it to the indexPath.row

 if let image = self.bookImages[indexPath.row]  {
cell.imageView.image = image
} else {
Alamofire.request(imageURLRequest).responseImage { response in
if let image = response.result.value {
cell.imageView.image = image
self.bookImages[indexPath.row] = image
} else {
cell.imageView.image = placeholderImage
}
}
}

Thank you for all your help. Hope this also helps someone who encountered the same problem.

Using Alamofire to load images using URLs from JSON

I would suggest you, to use SDWebImage, it also handles image caching out of the box:
https://github.com/rs/SDWebImage

It is very easy to use it basically:

import SDWebImage

// Get the image
cell.episodeImage.sd_setImage(with: URL(string: episodes[indexPath.row].imageURL))

Loading images very slow using Alamofire

You can simply load your images with advance cache system through SDWebImage like below:

let imageURL = URL(string: "youRemoteImageURL") //Image URL     
yourImageView.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "placeholder.png"))

Your tableViewCell will look like below:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.identifier, for: indexPath) as! TableViewCell

let item = data[indexPath.row]
customCell.AdTitle?.text = item["title"].string
customCell.AdDate?.text = item["time"].string

let imageUrl = item["document"].string
let url = NSURL(string:("https://mysite.co/uploads/" + imageUrl))
customCell.AdImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder.png"))

return customCell
}

Lazy loading of images using Alamofire and AlamofireImage Not working at first Load

The problem is image which downloaded from network is not refreshed to the cell after downloading. You need to callback in Alamofire.request block. Solution:

First, add the block to callback in model:

class Brands {
//......
public var imageDidDownload:(()->Void)? //Key point, declare a callback block

init(BrandsDict : Dictionary<String, AnyObject>){
if let imageUrl = BrandsDict["imageUrl"] as? String{
Alamofire.request(imageUrl).responseImage(completionHandler: { (response) in
//......
self._image = image
self.imageDidDownload?() //Key point, callback after image downloaded
//......
})
}else {
//......
}
}
}

Second, in cell, to handle the image downloaded callback to refresh the image:

class BrandsCVCell : UICollectionViewCell {
//......
func configureCell(_ brand : Brands){
self.brand = brand
self.brand.imageDidDownload = { [weak self]() -> Void in
self?.BrandImage.image = self?.brand.image //Key point, refresh image to the imageView after downloading.
}
BrandImage.image = self.brand.image
}
}

Try it, should work.

Get image data with Alamofire request

The folks over at Alamofire made an image component library, AlamofireImage. It handles all this stuff for you to make your life easier. Add it to your project and then you can just do this:

import Alamofire
import AlamofireImage

Alamofire.request(imageUrl, method: .get).responseImage { response in
guard let image = response.result.value else {
// Handle error
return
}
// Do stuff with your image
}


Related Topics



Leave a reply



Submit