1

I am getting image as PFFile. but when my table is scrolling every time PFFile is downloading and its take time. I want to cache my file so if its will be downloaded then it will get from cache not from the 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;

}
PAn Kaj Khatri
  • 539
  • 1
  • 6
  • 22

1 Answers1

0

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.

And all you have to do in the cellForRowAtIndexPath is load the image from the dictionary.

I think this will solve your problem

OverD
  • 2,612
  • 2
  • 14
  • 29
  • we cannot use PFFile as the UIImage if i am using the PFFile as UIImage i am getting the following error: Could not cast value of type 'PFFile' (0x10066f070) to 'UIImage' (0x19f48b590). – PAn Kaj Khatri Jan 05 '16 at 05:24
  • What I said was do you conversion from PFFile to UIimage inside the PFQuery and save it in the dictionary as UIImage....in the cellForRowAtIndexPath just call the image form the dictionary.....if you are still having issues with it send me the code of your PFQuery and I will help you out with it – OverD Jan 05 '16 at 08:42