从 Parse 中检索图像到 imageView (Swift)

Retrive an image into UiimageView from Parse (Swift)

我一直在尝试将 Parse.com 中的图像加载到故事板中的 uiimageview 中。

我上传了一张图片到 Parse。理想情况下,它会通过 ID 调用我的图像,然后继续在 uiimageView 中显示它。我到处搜索答案,但大多数要么是针对 Obj-C 的,要么只包含调用图像的一半代码。

我的目标是显示一张我可以每天更新的图片。

到目前为止,这是我的代码。它运行没有错误,但没有图像出现。

override func viewDidLoad() {
    super.viewDidLoad()

            //Print out a message if we're able to get to this part of the program.
    print("Reached getProductData function. Now asking for data.")
    getProductData()
    print("done");



}

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

func getProductData () //-> UIImage
{
    let query: PFQuery = PFQuery(className: "UserPhoto")
    var gridimage = UIImage()
    print("pass");
    //call function to get the data from parse by specifyng an objectId
    query.getObjectInBackgroundWithId("XXXXXXXXXX") {
        (productData:PFObject?, error:NSError?) -> Void in
        if error == nil && productData != nil {
            //Extract values from the productData PFObject and store them in constants
            //-----start image loading
            print("ok")
            if let productImageFile = productData!["productImage"] as? PFFile {
                productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
                    if error == nil {
                        //here I changed var gridimage to just gridimage
                        gridimage = UIImage(data:imageData!)!
                        self.eliegrid.image = UIImage(data:imageData!)
                        print("next")


                    }
                    else
                    {
                        print("error")
                    }
                }
            }

            //-----end image loading

        } else if error != nil {
            print("Could not load data from Parse")

        }


    }
    //return gridimage
}

}

它似乎跳过了这个块:

if let productImageFile = productData!["productImage"] as? PFFile {

编辑:因为它从不打印 "next".

我也看过了

但是还是不行

我对 Parse 和 Swift 都很陌生,我可能遗漏了一些简单的东西。如有任何帮助,我们将不胜感激!

更新:

谢谢@beyowulf!这是工作代码:

 func getProductData () 
{
    let query: PFQuery = PFQuery(className: "UserPhoto")
    var gridimage = UIImage()
    print("pass");
    //call function to get the data from parse by specifyng an objectId
    query.getObjectInBackgroundWithId("cF0h6RVcKu") {
        (productData:PFObject?, error:NSError?) -> Void in
        if error == nil && productData != nil {
            //Extract values from the productData PFObject and store them in constants
            //-----start image loading
            print("ok")
            print(productData)
            if let productImageFile = productData!["imageFile"] as? PFFile {
                productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
                    print("error")
                    if error == nil {
                        //here I changed var gridimage to just gridimage
                        gridimage = UIImage(data:imageData!)!
                        self.eliegrid.image = UIImage(data:imageData!)
                        print("next")



                    }
                }
            }


                    }
                    else
                    {
                        print("error")
                    }
                }
            }

}

移动:

func getProductData () -> UIImage
    {
        var gridimage = UIImage()
        print("pass");
        //call function to get the data from parse by specifyng an objectId
        query.getObjectInBackgroundWithId("XXXXXXXXXX") {
            (productData:PFObject?, error:NSError?) -> Void in
            if error == nil && productData != nil {
                //Extract values from the productData PFObject and store them in constants
                //-----start image loading
                print("ok")
                //var imageFromParse = object?.objectForKey("imageNews") as? PFFile
                //imageFromParse!.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                    //var image: UIImage! = UIImage(data: imageData!)!
                    //cell?.imageViewCell?.image = image
                //})
                if let productImageFile = productData!["productImage"] as? PFFile {
                productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
                        if error == nil {
                            var gridimage = UIImage(data:imageData!)!
                            self.eliegrid.image = UIImage(data:imageData!)
                            print("next")


                        }
                    }
                }



            } else if error != nil {
                print("Could not load data from Parse")

            }


        }
     return gridimage
    }

超出 viewDidLoad 然后删除:

print("done");
    self.eliegrid.image = getProductData()

从 viewDidLoad 的底部开始。您正在调用一个在可以执行完成块之前超出范围的函数,这就是它没有触发的原因。以这种方式嵌套函数并不是一件非常 swift-y 的事情,而且几乎从不建议。

也尝试更改:

if let productImageFile = productData!["productImage"] as? PFFile {
                productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
                        if error == nil {
                            var gridimage = UIImage(data:imageData!)!
                            self.eliegrid.image = UIImage(data:imageData!)
                            print("next")


                        }
                    }
                }

收件人:

                let productImageFile = productData.valueForKey("productImage") as! PFFile 
                productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
                        if error == nil {
                            var gridimage = UIImage(data:imageData!)!
                            self.eliegrid.image = UIImage(data:imageData!)
                            print("next")


                        }
                    }