Swift: download image from Internet and cache them doesn't work properly. need suggestions

梦想的初衷 提交于 2019-12-02 08:41:27

Use AlamofireImage

Swift 2.2 or 2.3

Add --> pod 'AlamofireImage', '~> 2.5' in your pod file.

Swift 3.1

Add --> pod 'AlamofireImage', '~> 3.3' in your pod file.

Put the following line in your code which will automatically cache the image and download it as well.

override func viewDidLoad() {
     super.viewDidLoad()
     //hit your web service here to get all url's
 }

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
   return arrImageURL.count
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{ 
   cell.imgViewPhoto.af_setImageWithURL(arrImageURL[indexPath.row], placeholderImage: UIImage())
}

Kingfisher is a lightweight and pure Swift implemented library for downloading and caching image from the web. This project is heavily inspired by the popular SDWebImage. And it provides you a chance to use pure Swift alternative in your next app. kingfisher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!