2015-12-17 4 views
1

Я получаю изображение как PFFile. но когда мой стол прокручивается каждый раз, когда PFFile загружается и его время занимает. Я хочу кэшировать свой файл, поэтому, если он будет загружен, он будет получен из кеша не из Parse.PIFile Image cache in Swift 2.0

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; 

} 
+0

вы можете добавить свой cellForRowAtIndexPath и вы используете PFQueryTableViewController или UITableViewController? – OverD

+0

Я использую UITableViewController –

+0

Можете ли вы прикрепить свой код к cellForRoAtIndexPath – OverD

ответ

0

Я посмотрел ваш код. Проблема заключается в PFFile.getDataInBackground() теперь, поскольку вы используете словари, я бы получил PFFile внутри вашего блока PFQuery и сохранил его в словаре как UImage() вместо PFFile.

И все, что вам нужно сделать в cellForRowAtIndexPath, загружает изображение из словаря.

Я думаю, что это решит вашу проблему

+0

мы не можем использовать PFFile как UIImage , если я использую PFFile, как UIImage я получаю следующее сообщение об ошибке: Не удалось бросить значение типа «PFFile» (0x10066f070) до «UIImage» (0x19f48b590). –

+0

То, что я сказал, это преобразование из PFFile в UIimage внутри PFQuery и сохранение его в словаре как UIImage .... в cellForRowAtIndexPath просто вызовите изображение из словаря ..... если у вас все еще есть проблемы с ним отправьте мне код вашего PFQuery, и я поправлю его с ним – OverD