画像を表示する tableView に取り組んでおり、ビューが読み込まれるたびに画像を再ダウンロードしています。画像を一度だけダウンロードするように画像をキャッシュする方法はありますか? (画像をキャッシュする)
1 に答える
1
あなたはこれを行うことができます:
tableViewDelegate で Cache オブジェクトを作成します
var myCache = NSCache()
次に、rowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let row = tableView.dequeueReusableCellWithIdentifier("MyCell") as? MyCell{
//Get the item
let item = items[indexPath.row]
//You need to cancel previous requests and reset the image to avoid render errors caused by the reusable row
//here you should also cancel any previous requests made to avoid downloading an image that the user won't see because he scrolled
cell.img.image = nil
//Search for the image in the cache
var img:UIImage?
img = myCache.objectForKey(item.image_url) as? UIImage
row.configureCell( item: item,img:img)
return row
}else{
return MyCell()
}
}
そしてついに独房へ
func configureCell(item:Item,image:UIImage?){
self.item = item
if image != nil{
//The image exist so you assign it to your UIImageView
img.image = image
}else{
//Create the request to download the image
}
}
于 2016-06-02T23:53:40.593 に答える