Skip to main content

How to Async Image download in swift 3

//Make a method by extension of class
//create any where

extension UIImageView {
    func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        contentMode = mode
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard
                let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                let data = data, error == nil,
                let image = UIImage(data: data)
                else { return }
            DispatchQueue.main.async() {
                self.image = image
            }
            }.resume()
    }
    func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        guard let url = URL(string: link) else { return }
        downloadedFrom(url: url, contentMode: mode)
    }

}

//usage by calling any where

 self.profileimgOut.downloadedFrom(link:"YOUR IMAGE URL")

Comments

Popular posts from this blog

post method using alamofire in swift 3

  func addSubscriptionApi(_completeUrl: String ) {         //HUD         Alamofire.request(_completeUrl, method:.post, parameters: nil , encoding: JSONEncoding. default , headers: nil ).responseJSON { response in             switch (response.result) {             case .success( _ ):                 if let data = response.result.value as ? NSDictionary                 {                     print(response.result.value as Any)                     print(data)                      print("SUCCESS")                  }                 break  ...

Set Placeholder in UiTextView in swift 3

//In viewdidLoad add this                DiscriptionTV . text = "Discription"         DiscriptionTV . textColor = UIColor . lightGray         DiscriptionTV . delegate = self          DiscriptionTV . layer . borderColor = UIColor . lightGray . cgColor ; then   //Mark:- textView Delegates          func textViewDidBeginEditing( _ textView: UITextView ) {         if DiscriptionTV . textColor == UIColor . lightGray {             DiscriptionTV . text = nil             DiscriptionTV . textColor = UIColor . white             DiscriptionLab . isHidden = false         }     }          func textViewDidEndEditing( _ textView: UITextView ) { ...