使用 Swift 从单元格中检索图像以在 FB/Twitter 上分享

Retrieving an image from a cell to share on FB/Twitter using Swift

我有一个自定义的 UITableViewCell,我通过首先将其下载为 follows

来填充图像
let url = NSURL(string: imageurl1[indexPath.row])

                let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in

                    if error != nil
                    {
                        //
                    }
                    else
                    {
                        dispatch_async(dispatch_get_main_queue(), {

                            if let image = UIImage(data: data!) {

                                cell.postedImage.image = image
                            }

                        })

                    }


                }
                task.resume()

这是 cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
    UITableViewCell{

        var cell = UITableViewCell()

        let cell = tableView.dequeueReusableCellWithIdentifier("feed1", forIndexPath: indexPath) as! feed

            cell.delegate = self

            if imageurl1[indexPath.row].isEmpty {

                cell.postedImage.image = UIImage(named: "no_image.jpg")

            }
            else
            {

                let url = NSURL(string: imageurl1[indexPath.row])

                let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in

                    if error != nil
                    {
                        cell.postedImage.image = UIImage(named: "no_image.jpg")
                                                }
                    else
                    {
                        dispatch_async(dispatch_get_main_queue(), {

                            if let image = UIImage(data: data!) {

                                cell.postedImage.image = image

                            }

                        })

                    }


                }
                task.resume()



            }
            cell.username.text = username1[indexPath.row]
            **cell.reportPress.tag = indexPath.row**
        }
return cell
}

图像已正确上传到适当的单元格中。我有一个共享按钮,按下该按钮将打开一个操作 sheet 并且它具有在 FB 等上共享的按钮。当 Facebook 内容弹出时,我可以添加默认文本。 现在我想从按下共享按钮的特定单元格中获取图像。我知道如何检测按下哪个单元格的哪个共享按钮,但我无法从该单元格获取图像,因为它没有存储在任何地方。

针对 fb 的操作 sheet

func report() {

    let actionSheetControllerIOS8: UIAlertController = UIAlertController()

        let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
            print("Cancel")
        }
        actionSheetControllerIOS8.addAction(cancelActionButton)

        let shareFBActionButton: UIAlertAction = UIAlertAction(title: "Share to Facebook", style: .Default)
        { action -> Void in
            print("FB shared")

            //////////////
            if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
                var fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

                fbShare.addImage(UIImage(named: "whatever.png"))

                fbShare.setInitialText("Hello")
                self.presentViewController(fbShare, animated: true, completion: nil)

            } else {
                var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
                self.presentViewController(alert, animated: true, completion: nil)
            }
            /////////////
        }
        actionSheetControllerIOS8.addAction(shareFBActionButton)

        self.presentViewController(actionSheetControllerIOS8, animated: true, completion: nil)

}

在feed.swift

中按下按钮时调用报告函数

这里是feed.swift

protocol MyCustomCellDelegator {
func report()
}

@IBAction func reportBut(sender: AnyObject) {

   self.delegate.report()
   print(reportPress.tag)

}

如您所见,我可以添加一张图片 whatever.png,它会在 facebook 弹出时显示。我是否应该将所有单元格的图像保存在某处,然后使用 indexPath.row 以某种方式访问​​它?欢迎提出任何建议!

将标签添加到与 cellForRowAtIndexPath 内同一单元格的 indexPath.row 相对应的共享按钮。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   fbShareButton.tag = indexPath.row
}

按下按钮后,您将获得对包含图像的单元格 indexPath.row 的引用,因此让我们获取图像。

//sender is you button tht is pressed
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! feed

//you can now access the image like so
cell.postedImage.image

如果您需要更多帮助,请告诉我