Loading images from parse.com

北慕城南 提交于 2019-12-13 07:56:09

问题


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:

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:


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.



来源:https://stackoverflow.com/questions/32012101/loading-images-from-parse-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!