从Parse,Asynchronous中恢复错误的图像

时间:2015-05-20 18:09:17

标签: image swift parse-platform

民间。我得到了解析的问题,我从Parse得到了一张图片,但是图片有问题,有时看起来是同一张图片,有的则出现错误的图像,我不知道发生了什么,有人帮助我!我是Parse和Swift的新手。谢谢!

导入UIKit 导入Parse 进口螺栓 进口螺栓 import Parse

类LojasViewController:UIViewController,UITableViewDelegate {

var ParseData = [PFObject]()
var dataObjects = [NSData]()
var photoArray: Array<UIImage> = []
var ImagemCelula = UIImage()


@IBOutlet weak var saloesTableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    GetParse()


}


override func viewDidAppear(animated: Bool) {
    // Refresh the table to ensure any data changes are displayed
    saloesTableView.reloadData()
}




// Retrieve from Parse
func GetParse() {

    var query:PFQuery = PFQuery(className:"Saloes")
    query.orderByAscending("nome")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            if let object = objects as? [PFObject] {
                self.ParseData = object

                println(self.ParseData.count)
                println(self.ParseData)
                self.saloesTableView.reloadData()

            }
        }
    }
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// Esconde statusBar
override func prefersStatusBarHidden() -> Bool {
    return true
}


@IBAction func voltarButton(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}





// MARK: - Table view data source ____________________________________

func numberOfSectionsInTableView(tableView: UITableView!) -> Int
{
    return 1
}




func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
    return ParseData.count
}




func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
{
    let cell:SaloesTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath!) as! SaloesTableViewCell

    cell.nomeSalao.text = ParseData[indexPath!.row]["nome"] as? String

    for object in ParseData {
        let ImageFile = object["imagemCelula"] as! PFFile
        ImageFile.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in

            if let imageData = imageData where error == nil
            {

                self.ImagemCelula = UIImage(data: imageData)!
                cell.imagemSalao.image = self.ImagemCelula
            }
        }
    }
    return cell
}

1 个答案:

答案 0 :(得分:0)

首先,您必须创建一个新数组,例如:

    var images = [NSData]()

或PFFile,UIImage等...

    var query = PFUser.query()
    query!.whereKey("yourWherekey", equalTo: "your where value")
    query?.findObjectsInBackgroundWithBlock{

        (results: [AnyObject]?, error) -> Void in

        if results != nil {

            for result in results!
            {
                self.images.append(result["image"] as! NSData)

                self.tableView.reloadData() // important


            }

        }
    }

将图像附加到图像阵列。

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cellA", forIndexPath: indexPath) as! UITableViewCell

    cell.textLabel?.text = "TEST STRING"
    cell.imageView?.image = UIImage(data: images[indexPath.row])

    return cell
}

并与indexPath.row连接。如果你想用PFFile编码,我用NSData编码