Swift 3: Display Image from URL

后端 未结 9 1858
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:38

    You could also use Alamofire\AlmofireImage for that task: https://github.com/Alamofire/AlamofireImage

    The code should look something like that (Based on the first example on link above):

    import AlamofireImage
    
    Alamofire.request("http://i.imgur.com/w5rkSIj.jpg").responseImage { response in
        if let catPicture = response.result.value {
            print("image downloaded: \(image)")
        }
    }
    

    While it is neat yet safe, you should consider if that worth the Pod overhead. If you are going to use more images and would like to add also filter and transiations I would consider using AlamofireImage

提交回复
热议问题