1

So I have been trying to retrieve an image from Parse since yesterday and I am very confused. I read similar questions and examples but all of them were people saving some image first, then retrieving it. What I want to do is simply retrieve an image that I manually uploaded to parse, I have been trying with this:

    //Retrieving image from parse
    var object = PFObject(className:"ClassImage")

    let getParseImg = object["image"] as! PFFile
    getParseImg.getDataInBackgroundWithBlock {
    (imageData: NSData?, error: NSError?) -> Void in

        if (error == nil){

            if let imageData = imageData{
            println("Retrieving Image")
            let img = UIImage(data:imageData)
            self.imageView.image = img

            }
        }
    }

In line two, it throws a "unexpectedly found nil while unwrapping an Optional value", I already tried "if let getParseImg", then it throws another error, in the end the line ended up looking like this:

if let getParseImg = object["image"] as? PFFile{

So after that, everything goes fine and compiles but it doesn't do absolutely anything, it does not get and display the image and it doesn't even print "Retrieving Image", any ideas how can I solve this, I am new to Parse so still getting familiarized with it.

Thanks in advance!

1 Answer 1

2

You need to run a query. Also, I used a dictionary called imagesDict in order to ensure your photos are returned in the correct order.

    var query = PFQuery(className: "ClassImage")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            let imageObjects = objects as! [PFObject]
            for (index, object) in enumerate(imageObjects) {
                let thumbnail = object["image"] as! PFFile
                thumbnail.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
                    if error == nil {
                        if let image = UIImage(data: imageData!) {
                            self.imagesDict[index] = image
                            self.theTableView.reloadData()

                        }
                    }
                }

            }

        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.