【问题标题】:PFFile Image cache in Swift 2.0Swift 2.0 中的 PFFile 图像缓存
【发布时间】:2016-03-23 10:38:12
【问题描述】:

我将图像作为 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;

}

【问题讨论】:

  • 你能添加你的 cellForRowAtIndexPath 吗?你使用的是 PFQueryTableViewController 还是 UITableViewController?
  • 我正在使用 UITableViewController
  • 你能把你的代码附加到 cellForRoAtIndexPath
  • 你在哪里有 PFQuery?如果您添加代码,我可以帮助您
  • 检查更新的代码

标签: swift image caching parse-platform pffile


【解决方案1】:

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

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

我想这会解决你的问题

【讨论】:

  • 如果我使用 PFFile 作为 UIImage,我们不能使用 PFFile 作为 UIImage 我收到以下错误:无法将类型“PFFile”(0x10066f070)的值转换为“UIImage”(0x19f48b590)。
  • 我说的是你是否在 PFQuery 中从 PFFile 转换为 UIimage 并将其作为 UIImage 保存在字典中...在 cellForRowAtIndexPath 中只需调用字典中的图像.....如果您仍然遇到问题,请将您的 PFQuery 代码发送给我,我会帮助您解决此问题
猜你喜欢
  • 2015-12-29
  • 1970-01-01
  • 2017-04-03
  • 2018-06-11
  • 2015-12-11
  • 2016-01-15
  • 2014-12-10
  • 2014-08-07
  • 2015-12-18
相关资源
最近更新 更多