I have the following function which downloads an image from server;
func getImageFromServerById(imageId: String) -> UIImage? {
let url:String = "https://dummyUrl.com/\(imageId).jpg"
var resultInNSDataformat: NSData!
let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in
if (error == nil){
resultInNSDataformat = data
}
}
task.resume()
return UIImage(data: resultInNSDataformat)
}
The function does not wait for the download task to be completed before returning the image. Therefore my app always crashes. Any ideas for how to wait for the download?