Swift 3:无法从Parse获取图像?

时间:2017-07-25 23:12:51

标签: swift image parse-platform swift3

好的,我已经查看过像How do you access an object's fields from a Parse query result in Swift?这样的问题,但答案是在Swift 3中无效。下面尝试从Parse中的第一个PFObject获取图像时出现错误:

  

无法转换值类型NSData,NSError - >无法预料   PFDataResultBlock?

var query = PFQuery(className: PARSE_CLASS!)
            query.order(byDescending: "createdAt")
            query.findObjectsInBackground {
                (objects, error) -> Void in

            if error == nil {
                //print(objects?.first?["testTxt"] as! NSString)
                //print(objects?.first?["testImg"] as! PFFile)

                let thumbnail = objects?.first?["testImg"] as! PFFile
                thumbnail.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
                    if error == nil {
                        if let image = UIImage(data: imageData!) {
//get image here

                        }
                    }
            }
        }

我尝试过更改类型和所有内容。如何在最近的Swift中存储来自Parse的图像?

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

if let validObjects = objects {
   for object in validObjects {
      let thumbnail = object["testImg"] as? PFFile
      thumbnail?.getDataInBackground (block: { (data, error) -> Void in
         //read image here
      }
   }
}