简体   繁体   English

Swift 2.0中的PFFile图像缓存

[英]PFFile Image cache in Swift 2.0

I am getting image as PFFile. 我正在将图像作为PFFile。 but when my table is scrolling every time PFFile is downloading and its take time. 但是当我的表每次PFFile下载时都在滚动并且花费时间。 I want to cache my file so if its will be downloaded then it will get from cache not from the Parse. 我想缓存我的文件,所以如果将其下载,那么它将从缓存而不是从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;

}

I had a look at your code. 我看了看你的代码。 The issue is the PFFile.getDataInBackground() now since you are using dictionaries I would get the PFFile inside your PFQuery block and and store it in the dictionary as UImage() instead of a PFFile. 现在的问题是PFFile.getDataInBackground(),因为您使用的是字典,所以我将PFFile放入PFQuery块中,并将其作为UImage()而不是PFFile存储在字典中。

And all you have to do in the cellForRowAtIndexPath is load the image from the dictionary. 在cellForRowAtIndexPath中,您要做的就是从字典中加载图像。

I think this will solve your problem 我认为这可以解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM