1

I am using PFQueryTableViewController to retrieve images from Parse.com backend. Later, I want to take a snapshot of swiped row image (using editActionsForRowAtIndexPath).

At this moment, I can retrieve object and create a action on row using editActionsForRowAtIndexPath. The action passes the retrivedObject through a prepareForSegue method to ShareViewController. Once I am on ShareViewController, I can see an image, but it is not the same I clicked to share using editActionsForRowAtIndexPath. It is either the image above or below or sometimes its the same one that i clicked.

Can anyone help me to solve this?

My code is as below:

PFQueryTableViewController

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
            var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell!
    if cell == nil {
        cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    }
    retrivedObject =  object

    // Display profile image
    let profileThumbnail = UIImage(named: "DProfilePicture")
    cell.profilePicture.image = profileThumbnail

    if let thumbnail = object?["profilePicture"] as? PFFile {
        cell.profilePicture.file = thumbnail
        cell.profilePicture.loadInBackground()
    }

    // Display main image
    let initialThumbnail = UIImage(named: "DefaultImage")
    cell.picture.image = initialThumbnail

    if let thumbnail = object?["picture"] as? PFFile {
        cell.picture.file = thumbnail
        cell.picture.loadInBackground()  
    }

    //........
}

   override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    print("EDIT MODE INDEX: \(indexPath.item)")

    let Share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
        print("Share button tapped")
        self.performSegueWithIdentifier("ShareImage", sender: self)   
    }
    Share.backgroundColor = UIColor.blackColor()
    return [Share]
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.destinationViewController is ShareViewController {
        let vc = segue.destinationViewController as! ShareViewController
        vc.selectedObject = retrivedObject
    }
}

ShareViewController

var selectedObject: PFObject!
var sharedImage: UIImage!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func viewDidAppear(animated: Bool) {
    if selectedObject == nil {
        return
    }

   if let file = selectedObject["picture"] as? PFFile {
                file.getDataInBackgroundWithBlock({ (data, error) -> Void in
                    if data != nil {
                self.sharedPicture.image = UIImage(data: data!)
                self.screenShotMethod()
             }
          })
       }
    }

@IBAction func cancelBtn(sender: AnyObject) {
   self.dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func shareBtn(sender: AnyObject) {
    let vc = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
    vc.setInitialText("Look at this great picture!")
    vc.addImage(sharedImage)
    self.presentViewController(vc, animated: true, completion: nil)
}

func screenShotMethod() -> UIImage {
    //Create the UIImage
    UIGraphicsBeginImageContextWithOptions(self.shareView.frame.size, true, 2.0 )
    self.shareView.drawViewHierarchyInRect(CGRect(x: 0, y: 0, width: self.shareView.bounds.width, height: self.shareView.bounds.height), afterScreenUpdates: false)
    view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    sharedImage = image
    return sharedImage
}
Vicky Arora
  • 501
  • 2
  • 7
  • 20

1 Answers1

0

The problem is your use of retrivedObject. By setting retrivedObject = object it is always set to the last object (cell) displayed. It is not related to the user selection.

When you create the share action you should set

self.performSegueWithIdentifier("ShareImage", sender: index) 

so that in the prepareForSegue function you can use the index (which is the sender) to get the true selected object.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I just checked. It is still showing me random image. Some time it will show the correct image, sometimes it wont. Is there anything else that I am missing here? – Vicky Arora Jan 22 '16 at 09:31
  • check the index you receive, is it the same as the one you tapped on? – Wain Jan 22 '16 at 09:34
  • Yes. It is the same. Do you think there is something related to how parse retrieved images? Or is there a way we can get only the selected cell instead of using retrivedObjects. I just printed the retrievedObject and can see it has different image than the one I selected. – Vicky Arora Jan 22 '16 at 09:45
  • you should now have deleted `retrivedObject`, it's the problem. follow my answer and use the index instead – Wain Jan 22 '16 at 09:47
  • I am sorry, if I remove the retrivedObject, how will I get the data? Anyways, I tried removing the retrivedObject. Now I am getting a blank screen on the next viewController. Correct me if I am wrong. – Vicky Arora Jan 22 '16 at 11:11
  • using the index that you pass to the segue as the sender, you use that to get the object from self with `objectAtIndexPath:` – Wain Jan 22 '16 at 11:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101395/discussion-between-vicky-arora-and-wain). – Vicky Arora Jan 22 '16 at 11:27