2015-05-27 55 views
0

我正在使用Swift中的一个应用程序,该应用程序使用固定行高为200的UITableViewController。我以320 x 200的原始分辨率存储在CloudKit上的JPG图像。它们的范围从25k到45k左右。使用CloudKit在后台加载图像?

我遇到的问题是表格填充时,它会在屏幕上加载2-3个单元格,如图所示。我希望剩下的单元格在后台加载,因为总共只有12个条目。未来可能会有多达25-30个。

现在正在工作,当我开始滚动时,正确的文本会加载到单元格中,但图像会稍微滞后一些,并会加载到先前显示的图像的顶部。

从做一些阅读,这是可重用细胞的结果?有没有办法加载背景中的所有单元格,以便它只在滚动后才加载图像?

下面是我得到了代码:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // Return the number of sections. 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // Return the number of rows in the section. 
    return traders.count 
} 



    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomTableViewCell 

    if traders.isEmpty { 
     // come back here later 
     return cell 
    } 

    let trader = traders[indexPath.row] 

    if let traderDate = trader.objectForKey("modificationDate") as? NSDate { 

    //format date 

    var dateFormatter = NSDateFormatter() 
    dateFormatter.dateFormat = "EEE MMM dd hh:mm a" 
    var dateString = dateFormatter.stringFromDate(traderDate) 
    cell.dateLabel?.text = dateString 
    } 

    cell.nameLabel?.text = trader.objectForKey("StringAttribute") as? String 
    cell.placeLabel?.text = trader.objectForKey("Place") as? String 
    println(cell.placeLabel.text) 

    // Can we can get the image from cache? 

    if let imageFileURL = imageCache.objectForKey(trader.recordID) as? NSURL { 
     println("Got image from cache") 
     cell.traderImageView?.image = UIImage(data: NSData(contentsOfURL: imageFileURL)!) 
    } else { 

     // OK then, fetch the image from CloudKit in the background 
     let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase 
     let fetchRecordsImageOperation = CKFetchRecordsOperation(recordIDs: [trader.recordID]) 
     fetchRecordsImageOperation.desiredKeys = ["image", "detailImage"] 
     fetchRecordsImageOperation.queuePriority = .VeryHigh 
     fetchRecordsImageOperation.perRecordCompletionBlock = {(record:CKRecord!, recordID:CKRecordID!, error:NSError!) -> Void in 
      if (error != nil) { 
       println("Failed to get image: \(error.localizedDescription)") 
      } else { 
       if let traderRecord = record { 
        dispatch_async(dispatch_get_main_queue(), { 
         if let imageAsset = traderRecord.objectForKey("image") as? CKAsset { 
          cell.traderImageView?.image = UIImage(data: NSData(contentsOfURL: imageAsset.fileURL)!) 
          self.imageCache.setObject(imageAsset.fileURL, forKey: trader.recordID) 
         } 

        }) 
       } 
      } 
     } 

     println("add operation") 
     publicDatabase.addOperation(fetchRecordsImageOperation) 
    } 
    println("return cell") 
    return cell 
} 

我假设必须有完成我后一种方式,和预加载所有的细胞图像。我已经考虑将代码中的图像作为资源存储在代码本身中(我认为它会加载得更快),但我真的想要通过CloudKit控制板更改图像,而无需每次重新提交新应用程序时每次我想更改图像。

回答

0

您应该查看缓存库,例如SDWebImageFastImageCache。出现滞后是由于几个原因 - jpg解码,从磁盘加载,以及Core Animation在内存中未对齐时的一些内存问题。

我更喜欢使用FastImageCache,因为它非常棒,并且令人难以置信地加载了图像加载。 This基准测试显示,在图像已经下载并保留在磁盘上的情况下,它的一个小优点。

相关问题