Swift 3: Display Image from URL

后端 未结 9 1868
Happy的楠姐
Happy的楠姐 2020-12-04 22:27

In Swift 3, I am trying to capture an image from the internet, and have these lines of code:

var catPictureURL = NSURL(fileURLWithPath: \"http://i.imgur.com/         


        
9条回答
  •  隐瞒了意图╮
    2020-12-04 22:53

    The easiest way according to me will be using SDWebImage

    Add this to your pod file

      pod 'SDWebImage', '~> 4.0'
    

    Run pod install

    Now import SDWebImage

          import SDWebImage
    

    Now for setting image from url

        imageView.sd_setImage(with: URL(string: "http://www.domain/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
    

    It will show placeholder image but when image is downloaded it will show the image from url .Your app will never crash

    This are the main feature of SDWebImage

    Categories for UIImageView, UIButton, MKAnnotationView adding web image and cache management

    An asynchronous image downloader

    An asynchronous memory + disk image caching with automatic cache expiration handling

    A background image decompression

    A guarantee that the same URL won't be downloaded several times

    A guarantee that bogus URLs won't be retried again and again

    A guarantee that main thread will never be blocked Performances!

    Use GCD and ARC

    To know more https://github.com/rs/SDWebImage

提交回复
热议问题