Loading/Downloading image from URL on Swift

前端 未结 30 2563
感动是毒
感动是毒 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 06:05

    FYI : For swift-2.0 Xcode7.0 beta2

    extension UIImageView {
        public func imageFromUrl(urlString: String) {
            if let url = NSURL(string: urlString) {
                let request = NSURLRequest(URL: url)
                NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
                (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
                    self.image = UIImage(data: data!)
                }
            }
        }
    }
    

提交回复
热议问题