Last active
August 29, 2015 14:18
-
-
Save fabnoe/2873ea69b867f548aedb to your computer and use it in GitHub Desktop.
Extension to load an external image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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