1

我正在使用PFQueryTableViewController从 Parse.com 后端检索图像。稍后,我想拍摄刷过的行图像的快照(使用editActionsForRowAtIndexPath)。

此时,我可以检索对象并使用editActionsForRowAtIndexPath. 动作retrivedObject通过一个prepareForSegue方法传递给ShareViewController。一旦我在 ShareViewController 上,我可以看到一个图像,但它与我单击共享使用editActionsForRowAtIndexPath. 它要么是上面或下面的图像,要么有时与我单击的图像相同。

谁能帮我解决这个问题?

我的代码如下:

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
    }
}

共享视图控制器

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
}
4

1 回答 1

0

问题是您使用retrivedObject. 通过设置retrivedObject = object,它始终设置为显示的最后一个对象(单元格)。它与用户选择无关。

创建共享操作时,您应该设置

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

以便在prepareForSegue函数中您可以使用index(即sender)来获取真正的选定对象。

于 2016-01-22T09:22:14.507 回答