我試圖在從XML
enclosure
下載數組後追加圖像。 20圖像在XML
。我想將這些圖像按順序排列在一個陣列中,然後嘗試保存在NSUserDefaults
中。 任何人都可以告訴我如何做到這一點? 謝謝在數組中添加圖像
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) {
// 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!)!)
}
}
}
}
'我得到這個錯誤後,一些輸出。圖像下載 圖像下載 圖像的數據 致命錯誤追加:數組索引超出範圍」
執行插入操作後,調試器會告訴您有關陣列的信息? – Feldur