I am initiating a download of images from an external URL and then saving them to the app's documents directory for future loading into table view cells.
The images load successfully after the initial download and seem to be saved to the correct location (?), but when loading them from the directory path, I am just seeing a blank image view. Can anyone help me know what exactly I am doing wrong?
var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
var dirPath = paths.stringByAppendingPathComponent("images/\(self.contentids[indexPath.row])/")
var imagePath = paths.stringByAppendingPathComponent("images/\(self.contentids[indexPath.row])/\(self.images[indexPath.row])")
var checkImage = NSFileManager.defaultManager()
var remoteImage = "\(baseUrl+self.images[indexPath.row])"
if checkImage.fileExistsAtPath(imagePath) {
let getImage = UIImage(contentsOfFile: imagePath)
cell?.cellImageView.image = getImage
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
checkImage.createDirectoryAtPath(dirPath, withIntermediateDirectories: true, attributes: nil, error: nil)
let getImage = UIImage(data: NSData(contentsOfURL: NSURL(string: remoteImage)!)!)
UIImageJPEGRepresentation(getImage, 100).writeToFile(imagePath, atomically: true)
dispatch_async(dispatch_get_main_queue()) {
cell?.cellImageView.image = getImage
return
}
}
}
Aucun commentaire:
Enregistrer un commentaire