I am trying to append image with its data in Array after downloading and then try to save in NSUserDefaults. But getting an error. I dont know what is proper way to save and read it . Can anyone please tell me how i can do this? Thanks
var imgIndex = 0
var imageArray : [UIImage] = []
typealias CompletionHandler = (image: UIImage) -> Void
downloadFileFromURL(NSURL(string: self.posts.objectAtIndex(indexPath.row).valueForKey("enclosure") as! String)!, completionHandler:{(img) in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.sideImageView.image = img
imageArray.insert(img, atIndex: self.imgIndex) //.append(img)
self.imgIndex++
print("Image append with data")
self.newsDefaults.setObject(imageArray, forKey: "image")
})
})
func downloadFileFromURL(url1: NSURL?,completionHandler: CompletionHandler) {
// download code.
if let url = url1{
let priority = DISPATCH_QUEUE_PRIORITY_HIGH
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let data = NSData(contentsOfURL: url)
if data != nil {
print("image downloaded")
completionHandler(image: UIImage(data: data!)!)
}
}
}
}
You can't add images to user defaults, they aren't supported. You'll need to convert the image to data and save that instead (either to user defaults or, better, onto disk in a file...
imageArray.insert(UIImageJPEGRepresentation(img, 0.75), atIndex: self.imgIndex)