Loading/Downloading image from URL on Swift

前端 未结 30 2576
感动是毒
感动是毒 2020-11-21 05:39

I\'d like to load an image from a URL in my application, so I first tried with Objective-C and it worked, however, with Swift, I\'ve a compilation error:

30条回答
  •  你的背包
    2020-11-21 05:55

    I recommend using Kingfisher library to download images asynchronously. The best part about using Kingfisher is, it caches all the downloaded images by default with the image url as an id. Next time when you request to download image with that particular URl, it will load it from cache.

    Usage:

    newsImage.kf.setImage(with: imageUrl!, placeholder: nil, options: nil, progressBlock: nil, completionHandler: { (image, error, cacheType, imageUrl) in
                    if error == nil{
                        self.activityIndicator.stopAnimating()
                    }else if error != nil{
                        self.activityIndicator.stopAnimating()
                    }
                })
    

提交回复
热议问题