Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active December 1, 2015 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FokkeZB/81dde872cf1b1fd217cd to your computer and use it in GitHub Desktop.
Save FokkeZB/81dde872cf1b1fd217cd to your computer and use it in GitHub Desktop.
How to extend WKInterfaceImage with a method to load remote images on watchOS

How to extend WKInterfaceImage with a method to load remote images on watchOS

Usage:

myImage.setImageWithUrl(myUrl, 2.0)

Prerequisites

Make sure you whitelist the domains in the Info.plist or allow all with:

	<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<true/>
	</dict>
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment