I am trying to add images to an array after loading from XML enclosure. 20 images are in XML. I want to save these images one at a time in an array on the order form, and then try to save to NSUserDefaults. Can someone tell me how can I do this? Thanks
var imageArray : [NSData] = []
var imgIndex = 0
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
if indexPath.row == self.imgIndex{
imageArray.insert(UIImageJPEGRepresentation(img, 0.75)!, atIndex: self.imgIndex)
self.imgIndex++
print("Image append with data")
self.newsDefaults.setObject(imageArray, forKey: "image")
}
})
})
func downloadFileFromURL(url1: NSURL?,completionHandler: CompletionHandler) {
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!)!)
}
}
}
}
'I get this error after some output. image uploaded image uploaded Image added with data fatal error: array index out of range "
source
share