簡體   English   中英

Swift 2.0中的PFFile圖像緩存

[英]PFFile Image cache in Swift 2.0

我正在將圖像作為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;

}

我看了看你的代碼。 現在的問題是PFFile.getDataInBackground(),因為您使用的是字典,所以我將PFFile放入PFQuery塊中,並將其作為UImage()而不是PFFile存儲在字典中。

在cellForRowAtIndexPath中,您要做的就是從字典中加載圖像。

我認為這可以解決您的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM