Skip to content

Instantly share code, notes, and snippets.

@fabnoe
Last active August 29, 2015 14:18
Show Gist options
  • Save fabnoe/2873ea69b867f548aedb to your computer and use it in GitHub Desktop.
Save fabnoe/2873ea69b867f548aedb to your computer and use it in GitHub Desktop.
Extension to load an external image
//
// InterfaceController.swift
// AsnycCall WatchKit Extension
//
// Created by Fabian Nöthe on 05.04.15.
// Copyright (c) 2015. All rights reserved.
//
import WatchKit
class InterfaceController: WKInterfaceController {
@IBOutlet weak var imageView: WKInterfaceImage!
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}
override func willActivate() {
super.willActivate()
let image_url:String = "http://placehold.it/350x150"
self.imageView.setImageWithUrl(image_url)
}
override func didDeactivate() {
super.didDeactivate()
}
}
//
// WKInterfaceImage+SetImageWithUrl.swift
// AsnycCall
//
// Created by Fabian Nöthe on 05.04.15.
// Copyright (c) 2015. All rights reserved.
//
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment