Commit bec6cbeb by Daniel Dahan

Updated example project to include async image loading with updated internals using NSURLSession.

parent eff18099
...@@ -42,5 +42,20 @@ ...@@ -42,5 +42,20 @@
<string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string> <string>UIInterfaceOrientationLandscapeRight</string>
</array> </array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>cosmicmind.io</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
</dict> </dict>
</plist> </plist>
...@@ -60,10 +60,17 @@ class ViewController: UIViewController { ...@@ -60,10 +60,17 @@ class ViewController: UIViewController {
*/ */
private func prepareGeneralMaterialLayerExample() { private func prepareGeneralMaterialLayerExample() {
let materialLayer: MaterialLayer = MaterialLayer(frame: CGRectMake(132, 132, 150, 150)) let materialLayer: MaterialLayer = MaterialLayer(frame: CGRectMake(132, 132, 150, 150))
materialLayer.image = UIImage(named: "CosmicMindAppIcon")
materialLayer.shape = .Circle materialLayer.shape = .Circle
materialLayer.depth = .Depth2 materialLayer.depth = .Depth2
UIImage.contentsOfURL(NSURL(string: "http://www.cosmicmind.io/CosmicMind.png")!) { (image: UIImage?, error: NSError?) in
if let v: UIImage = image {
materialLayer.image = v
} else {
materialLayer.image = UIImage(named: "CosmicMindAppIcon")
}
}
// Add materialLayer to UIViewController. // Add materialLayer to UIViewController.
view.layer.addSublayer(materialLayer) view.layer.addSublayer(materialLayer)
......
...@@ -32,16 +32,20 @@ import UIKit ...@@ -32,16 +32,20 @@ import UIKit
public extension UIImage { public extension UIImage {
/** /**
:name: contentsOfURL Asynchronously load images with a completion block.
- Parameter URL: A URL destination to fetch the image from.
- Parameter completion: A completion block that is executed once the image
has been retrieved.
*/ */
public class func contentsOfURL(URL: NSURL, completion: ((image: UIImage?, error: NSError?) -> Void)) { public class func contentsOfURL(URL: NSURL, completion: ((image: UIImage?, error: NSError?) -> Void)) {
let request: NSURLRequest = NSURLRequest(URL: URL) NSURLSession.sharedSession().dataTaskWithRequest(NSURLRequest(URL: URL)) { (data: NSData?, response: NSURLResponse?, error: NSError?) in
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in dispatch_async(dispatch_get_main_queue()) {
if let v: NSError = error { if let v: NSError = error {
completion(image: nil, error: v) completion(image: nil, error: v)
} else if let v: NSData = data { } else if let v: NSData = data {
completion(image: UIImage(data: v), error: nil) completion(image: UIImage(data: v), error: nil)
}
} }
} }.resume()
} }
} }
\ No newline at end of file
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
import UIKit import UIKit
public enum ContentImageFormatType { public enum ImageFormatType {
case PNG case PNG
case JPEG case JPEG
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment