The code below works for downloading a single image from a url.
func imageForImageURLString(imageURLString: String, completion: (image: UIImage?, success: Bool) -> Void) {
guard let url = NSURL(string: imageURLString),
let data = NSData(contentsOfURL: url),
let image = UIImage(data: data)
else {
completion(image: UIImage(named:"absolut5.png"), success: false);
return
}
completion(image: image, success: true)
}
however some of the urls I am working with are a zipped folder with several png files inside. So I have adjusted the above code to just bring back the data. Which seems to work
func dataForDataURLString(imageURLString: String, completion: (data: NSData?, success: Bool) -> Void) {
guard let url = NSURL(string: imageURLString),
let data = NSData(contentsOfURL: url)
else {
return
}
completion(data: data, success: true)
}
usage
let imageUrlString = item.valueForKey(url) as! String
self.dataForDataURLString(imageUrlString, completion: { (data, success) -> Void in
if success {
guard let dataDocs = data
else { return } // Error handling here
documentData = dataDocs
} else {
// Error handling here.
}
})
But I have not found anything to unzip/loop through the contents of the returned data. It is not in a JSON format, just raw data strings of numbers when printed.
Any help would be appreciated (it is probably worth mentioning that I am doing all this in a background NSOperationQueue with the completion block saving the result in core data. I have looked at a few NSURLSessionDownloadTask options, but these seem to go onto a different thread, and still don't work)