Created
August 29, 2017 07:49
-
-
Save thrasos/2c1107dfa6f2524f715000068bca1a80 to your computer and use it in GitHub Desktop.
Synchronous Request for Loading images from URL String in Swift
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
func loadImageSynchronouslyFromURLString(_ urlString: String) -> UIImage? { | |
if let url = URL(string: urlString) { | |
let request = NSMutableURLRequest(url: url) | |
request.timeoutInterval = 30.0 | |
var response: URLResponse? | |
let error: NSErrorPointer? = nil | |
var data: Data? | |
do { | |
data = try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: &response) | |
} catch let error1 as NSError { | |
error??.pointee = error1 | |
data = nil | |
} | |
if (data != nil) { | |
return UIImage(data: data!) | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment