Loading images from parse.com

2019-08-28 12:01发布

This is my saving image to parse:

func uploadPost(){
        var imageText = self.imageText.text

        if (imageView.image == nil){
            println("No image uploaded")
        }
        else{
            var posts = PFObject(className: "Posts")
            posts["imageText"] = imageText
            posts["uploader"] = PFUser.currentUser()
            posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                if error == nil{
                    //**Success saving, now save image.**//

                    // Create an image data
                    var imageData = UIImagePNGRepresentation(self.imageView.image)
                    // Create a parse file to store in cloud
                    var parseImageFile = PFFile(name: "upload_image.png", data: imageData)
                    posts["imageFile"] = parseImageFile
                    posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                        if error == nil{
                            // Take user home
                            println("Data uploaded")
                        }
                        else{
                            println(error)
                        }
                    })
                }
                else{
                    println(error)
                }
            })
        }
    }

How can I load the images from parse? This is how my Parse.com "Posts" data looks like: Image

Any suggestions?

I think maybe something like using this:

self.imageView.sd_setImageWithURL(url, completed: block)

But I don´t know how I get the URL. And what if the images has different names?

1条回答
Ridiculous、
2楼-- · 2019-08-28 12:34

try something like this

let imageFile = pfObject["imageFile"] as? PFFile
    if imageFile != nil{
        imageFile!.getDataInBackgroundWithBlock {
            (imageData: NSData?, error: NSError?) -> Void in
            if error == nil {
                if let imageData = imageData {
                    self.imageView.image = UIImage(data: imageData)!
                }
             }
         }
     }

Where pfObject is the reference to your object. There are other ways you could check for a nil value, but this should work.

As long as you have a reference to the correct object (which you can get via a query for objectId) then you should only need the name of the column the image is file is stored in and not the image file itself.

查看更多
登录 后发表回答