Download image from WatchKit URL

Is there a more elegant solution for loading an external image onto a watch than the following?

let image_url:String = "http://placehold.it/350x150" dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let url:NSURL = NSURL(string:image_url)! var data:NSData = NSData(contentsOfURL: url)! var placeholder = UIImage(data: data)! // update ui dispatch_async(dispatch_get_main_queue()) { self.imageView.setImage(placeholder) } } 
+6
source share
6 answers

NSURL is for local files. Use NSURLSession instead. It is also useful to set the scale for the remote image.

 import WatchKit public extension WKInterfaceImage { public func setImageWithUrl(url:String, scale: CGFloat = 1.0) -> WKInterfaceImage? { NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) { data, response, error in if (data != nil && error == nil) { let image = UIImage(data: data!, scale: scale) dispatch_async(dispatch_get_main_queue()) { self.setImage(image) } } }.resume() return self } } 

Use it like this:

 self.imageView.setImageWithUrl(image_url, scale: 2.0) 
+10
source

Here is the category

 import WatchKit public extension WKInterfaceImage { public func setImageWithUrl(url:String) -> WKInterfaceImage? { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let url:NSURL = NSURL(string:url)! var data:NSData = NSData(contentsOfURL: url)! var placeholder = UIImage(data: data)! dispatch_async(dispatch_get_main_queue()) { self.setImage(placeholder) } } return self } } 

Use it like this:

  self.imageView.setImageWithUrl(image_url) 
+4
source

I think this is good because it can help your application get out of time when you try to download some images from the Internet. you can make a new function as follows:

 func loadImage(url:String, forImageView: WKInterfaceImage) { // load image let image_url:String = url dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let url:NSURL = NSURL(string:image_url)! var data:NSData = NSData(contentsOfURL: url)! var placeholder = UIImage(data: data)! // update ui dispatch_async(dispatch_get_main_queue()) { forImageView.setImage(placeholder) } } } 

after that, where you want to load the image from urlString, you can use this:

 loadImage("http://...", forImageView: self.myImageView) 

I hope for this help.

+3
source

I think that with this solution you can store the image in the cache and display the image from the cache. You can also call this function and use it.

 func loadImage(url:String, forImageView: WKInterfaceImage) { forImageView.setImageNamed("placeholder") let image_url:String = url dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let url:NSURL = NSURL(string:image_url)! print(url) //if image is already stored in cache if WKInterfaceDevice.currentDevice().cachedImages[image_url] != nil{ dispatch_async(dispatch_get_main_queue()) { forImageView.setImageNamed(image_url) } }else{ if let data = NSData(contentsOfURL: url){ //load image let image = UIImage(data: data)! //Store image in cache WKInterfaceDevice.currentDevice().addCachedImage(image, name: image_url) dispatch_async(dispatch_get_main_queue()) { forImageView.setImage(placeholder) } } } } } 
+2
source
  if let url = NSURL(string: "http://google.net/img/upload/photo2.png") { if let data = NSData(contentsOfURL: url){ imageWK.setImage(UIImage(data: data)) } } 

Try this code. Remember to add NSTransportSecurity to your Plist.

0
source
 public extension WKInterfaceImage { public func setImageWithUrl(url:String, scale: CGFloat = 1.0) -> WKInterfaceImage? { URLSession.shared.dataTask(with: NSURL(string: url)! as URL) { data, response, error in if (data != nil && error == nil) { let image = UIImage(data: data!, scale: scale) DispatchQueue.main.async() { self.setImage(image) } } }.resume() return self } 

}

0
source

Source: https://habr.com/ru/post/984706/


All Articles