2016-01-21 63 views
0

我正在構建一個基於圖片的應用程序。所以對於我像Instagram一樣異常地檢索所有圖像是非常重要的。我明白這個功能...Async With Parse

var query = PFQuery(className: "Post") 
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in 
     if let objects = objects as! [PFObjects] { 
      for object in objects { 
       objectsArray.append(object) 
      } 
     } 
    } 

...是異步的。但我想要一種方法來將Parse中的圖像異步加載到表中,以便在滾動時加載圖像。

回答

2

你應該看看PFImageView的功能loadInBackground()

例如,如果您使用的是PFTableViewController與PFTableViewCell,你可以做以下

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell { 

    var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell! 
    if cell == nil { 
     cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell") 
    } 

    if let name = object?["name"] as? String { 
     cell.nameLbl.text = name 
    } 

    var initialThumbnail = UIImage(named: "placeholder") 
    cell.imgView.image = initialThumbnail 
    if let thumbnail = object?["photo"] as? PFFile { 
     cell.imgView.file = thumbnail 
     cell.imgView.loadInBackground() 
    } 

    return cell 
} 

與PFTableViewCell有

class CustomCell: PFTableViewCell { 
    @IBOutlet weak var nameLbl: UILabel! 
    @IBOutlet weak var imgView: PFImageView! 
} 
從另一個SO reply

此外,你可以試試這個:

let userImageFile = userPhoto["imageFile"] as PFFile 
userImageFile.getDataInBackgroundWithBlock { 
    (imageData: NSData!, error: NSError!) -> Void in 
    if !error { 
     let image = UIImage(data:imageData) 
    } 
} 
+0

謝謝!這有助於很多! –