Loading/Downloading image from URL on Swift

前端 未结 30 2574
感动是毒
感动是毒 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:57

    Swift 4.2 and AlamofireImage

    If using a library is not an issue, you can do it by help of the AlamofireImage. my samples are from its Github

    Placeholder Images Example:

    let imageView = UIImageView(frame: frame)
    let url = URL(string: "https://httpbin.org/image/png")!
    let placeholderImage = UIImage(named: "placeholder")!
    imageView.af_setImage(withURL: url, placeholderImage: placeholderImage)
    

    it has many handy functions and extension to work with images. from caching to scaling and resizing or even applying filters on the image. if images are important in your app, I suggest to use this framework and save your time.

提交回复
热议问题