用Swift从解析中检索图像

时间:2021-12-15 16:28:47

I'm trying to retrieve an image and text from Parse. I'm able to retrieve the saved text but not the image. What am I doing wrong? Below is the code for the function that I want to retrieve the image. Thanks in advance.

我试图从Parse中检索图像和文本。我可以检索已保存的文本,但不能检索图像。我做错了什么?下面是我要检索图像的函数的代码。提前谢谢。

func showImage() {
    var query = PFQuery(className: "Description")
    query.orderByDescending("ceatedAt")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

        self.imageArray = [UIImage]()
        if let objects = objects {
            for imageObject in objects {

                let userImage: UIImage? = (imageObject as! PFObject)["UserPhoto"] as? UIImage
                if userImage != nil {
                    self.imageArray.append(userImage!)
                }
            }

        }
        self.tableView.reloadData()

    }



}

1 个解决方案

#1


2  

It's tricky at first, but it gets a lot easier. Here's the code to make it work:

一开始很棘手,但现在容易多了。下面是让它工作的代码:

    let userImage = (imageObject as! PFObject)["UserPhoto"] as! PFFile 


    userImage.getDataInBackgroundWithBlock {
        (imageData, error) -> Void in


        if error == nil {
            let image = UIImage(data: imageData!)

            self.imageArray.append(userImage!)

        } else {}
        }}

the issue is that parse stores images as PFFiles, they're not directly images yet, think of it more as a URL than anything. You have to download the image, and then do something with it to make it work. You can't just directly cast it as a UIImage.

问题是,解析将图像存储为PFFiles,它们还不是直接的图像,把它看作是一个URL,而不是任何东西。你必须下载图像,然后用它做些什么来让它工作。你不能直接将它转换成UIImage。

One thing to note (because this gave me trouble a while ago) is that the .getDataInBackgroundWithBlock method is asynchronous, so it'll run on it's own, and your code will continue before it's completed. Another thing to get used to.

需要注意的一件事(因为刚才这给我带来了麻烦)是. getdatainbackgroundwithblock方法是异步的,所以它将自己运行,您的代码将在它完成之前继续运行。另一件要习惯的事。

Best of luck!

最好的运气!

#1


2  

It's tricky at first, but it gets a lot easier. Here's the code to make it work:

一开始很棘手,但现在容易多了。下面是让它工作的代码:

    let userImage = (imageObject as! PFObject)["UserPhoto"] as! PFFile 


    userImage.getDataInBackgroundWithBlock {
        (imageData, error) -> Void in


        if error == nil {
            let image = UIImage(data: imageData!)

            self.imageArray.append(userImage!)

        } else {}
        }}

the issue is that parse stores images as PFFiles, they're not directly images yet, think of it more as a URL than anything. You have to download the image, and then do something with it to make it work. You can't just directly cast it as a UIImage.

问题是,解析将图像存储为PFFiles,它们还不是直接的图像,把它看作是一个URL,而不是任何东西。你必须下载图像,然后用它做些什么来让它工作。你不能直接将它转换成UIImage。

One thing to note (because this gave me trouble a while ago) is that the .getDataInBackgroundWithBlock method is asynchronous, so it'll run on it's own, and your code will continue before it's completed. Another thing to get used to.

需要注意的一件事(因为刚才这给我带来了麻烦)是. getdatainbackgroundwithblock方法是异步的,所以它将自己运行,您的代码将在它完成之前继续运行。另一件要习惯的事。

Best of luck!

最好的运气!