1

我将图像作为 PFFile 获取。但是当每次 PFFile 下载时我的表格都在滚动并且需要时间。我想缓存我的文件,所以如果要下载它,那么它将从缓存中获取,而不是从 Parse 获取。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! RidesCustomCell!
    if cell == nil {
        cell = RidesCustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    }

    let currentDic = dataArray.objectAtIndex(indexPath.row) as! NSDictionary

    cell.backgroundColor = UIColor.clearColor()

    cell.txtName.text = currentDic.valueForKey("firstName") as? String
    cell.txtPrice.text = currentDic.valueForKey("price") as? String
    cell.txtTime.text = currentDic.valueForKey("date")as? String
    cell.txtFrom.text =  currentDic.valueForKey("from") as? String
    cell.txtTo.text = currentDic.valueForKey("to") as? String


    print(currentDic)
    cell.imgUser.image = UIImage(named: "noImg.png")


    if (currentDic.valueForKey("imageFile") != nil){

   //     let userImageFile = currentDic.valueForKey("imageFile") as! PFFile



        let queue : dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

        dispatch_async(queue, { () -> Void in
            print(currentDic)

            let pfuserGet =  currentDic.valueForKey("user") as! PFUser
            print(pfuserGet.username)
            print(pfuserGet.email)
            print(pfuserGet.password)


            let userImageFile = pfuserGet.objectForKey("profilePicture") as! PFFile
            userImageFile.getDataInBackgroundWithBlock {
                (imageData: NSData?, error: NSError?) -> Void in
                if error == nil {
                    if let imageData = imageData {
                        let image = UIImage(data:imageData)
                        cell.imgUser.image = image
                    }
                }
            }

        })

    }


    return cell;

}
4

1 回答 1

0

我看了你的代码。现在的问题是 PFFile.getDataInBackground(),因为您使用的是字典,所以我会在 PFQuery 块中获取 PFFile,并将其作为 UImage() 而不是 PFFile 存储在字典中。

而在 cellForRowAtIndexPath 中您所要做的就是从字典中加载图像。

我认为这将解决您的问题

于 2015-12-31T13:16:54.147 回答