How to pass a pfimageview from one controller to another in storyboard with swift and parse.com

181 views Asked by At

I was hoping I could have some advice. I'm making a quiz app, which contains images. The quiz has 4 answer boxes with a single image view. I can download the images into the quiz via parse.com, however I now want to be able to click on the image which then goes to a separate view controller so you can zoom into the image in full screen. I have been able to call the pffile and segue it, however my app crashes when I try and getdatainbackgroundwithblock in the second view controller. How do you think I should segue the image?

EDIT

Yes sorry. So the crash I was getting was an error at code=1 address=0x48, which occurred at line +55 of getdatainbackgroundwithblock.

I have pasted snipped of the code I was using below.

I used this to call the image in the first view and make it into an image in my PFImageView.

`for object : PFObject in objectsArray as! [PFObject] { let image = object["Image"] as! PFFile let transimage = image

                image.getDataInBackgroundWithBlock {
                    (imageData:NSData?, error:NSError?) -> Void in
                    if error == nil {
                        let finalimage = UIImage(data: imageData!)
                        //  self.imageView.append(finalimage!)
                        self.imageView.image = final image`

I then passed it to my second view controller like this.

`override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if(segue.identifier == "ZoomImage"){

    // Get the new view controller using [segue destinationViewController].
    var detailScene = segue.destinationViewController as! ZoomImageViewController



            detailScene.currentObject = transimage as! PFFile

        NSLog("checkone")

    }
}`

In my second view controller I tried to make it back into an image like this.

` if let object = currentObject { NSLog("(currentObject)")

        var initialThumbnail = UIImage(named: "question")
        NSLog("check1")
        imageView.image = initialThumbnail
        NSLog("check2")
        NSLog("check3")

        object.getDataInBackgroundWithBlock {

            (imageData:NSData?, error:NSError?) -> Void in
            if error == nil {
            NSLog("check4")
            let finalimage = UIImage(data: imageData!)
            //  self.imageView.append(finalimage!)
            self.imageView.image = finalimage
            NSLog("check5")`

I would get an NSLog of 3, and then the code would break. The NSLog for the currentObject also brought back a PFFile, so I believed it to be true. Where am I going wrong? And is there a better way to pass it?

2

There are 2 answers

0
Kailas198 On

You were right, I didn't need getdata. I got rid of that and instead of seguing my pffile I segued the pfobject carrying the image and the text. I then called the object as a file and put it in the uiimageview. I'll post the code I used in a bit incase anyone wants it.

0
Lory Huz On

Why you call getDataInBackgroundWithBlock in both view controllers ? You already fetch the data in your PFFile instance so no need to call it twice. It's could be the issue.