Screenshot of a UITableViewCell from swift

0

I'm doing an app in swift , in which I need to be able to make a screenshot of a specific cell of a table from a button.

I have this code:

func capturarPantalla(sender: UITableViewCell) {
        let miDelegate = (UIApplication.sharedApplication().delegate! as! AppDelegate)
        UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: {
            miDelegate.window?.alpha = 0
            let sound = NSURL (fileURLWithPath: NSBundle.mainBundle().pathForResource("sonido", ofType: "mp3")!)
            do {
                self.player = try AVAudioPlayer (contentsOfURL: sound)
                print("Reproduciendo sonido")
            } catch let error as NSError {
                print("Hay un error \(error)")
            }
            self.player.prepareToPlay()
            self.player.play()
            }, completion: { finished in
                UIGraphicsBeginImageContextWithOptions(sender.bounds.size, false, UIScreen.mainScreen().scale)
                sender.layer.renderInContext(UIGraphicsGetCurrentContext()!)
                let image = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()

                UIImageWriteToSavedPhotosAlbum(image, self, #selector(PantallaNoticiasTableViewController.image(_:didFinishSavingWithError:contextInfo:)), nil)
                miDelegate.window!.alpha = 1
        })
    }

    func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeMutablePointer<Void>) {
        if error != nil {
            print("KO")
        }
        else {
            print("OK")
        }
    }

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell()
    let currentArticleToDisplay:Articulo = self.articles[indexPath.row]
    var btnCaptura: UIButton = UIButton()

    cell.backgroundColor = AppDelegate().getColorFondo()
    cell.sizeToFit()

    btnCaptura = cell.viewWithTag(20) as! UIButton
    let tapGestureCa = UITapGestureRecognizer(target: self, action: #selector(capturarPantalla(_:)))
    tapGestureCa.numberOfTapsRequired = 1
    btnCaptura.userInteractionEnabled = true
    btnCaptura.addGestureRecognizer(tapGestureCa)
}

But I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell capturarPantalla:]: unrecognized selector sent to instance 0x13895a5b0'

How can I pass a parameter in a selector

EDITED

I added the solution of @mhergon but now I have an error here, this error does not happen in all the cells, only in some:

btnCaptura = cell.viewWithTag(20) as! UIButton

Error images:

PUT TAG 99 BUT IT IS BECAUSE I HAVE CHANGED IT GLOBALLY BY MEASURE. ON THE TABLE VIEW CELL IS ALSO 99

    
asked by 08.09.2016 в 08:16
source

1 answer

0

You have a pretty big mess of concepts. To begin with, it does not make sense to add a UITapGestureRecognizer to a UIButton since it is a button. That is, you must add the option to the button as follows:

btnCaptura.addTarget(self, action: #selector(capturarPantalla(_:)), forControlEvents: .TouchUpInside)

And changing the capture method to:

func capturarPantalla(sender: UIButton) {

    /**
     *  Código...
     */

}

Next, you have to know which cell you are in in order to be able to capture it. Here are two ways, the first is looking for superview of sender since the button is inside a UITableViewCell etc. The second, and more correct and secure is the following:

First, assign a tag to the button that will be the indexPath of the cell:

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

    var btnCaptura: UIButton = UIButton()
    btnCaptura.tag = indexPath.row

}

In this way, in the capture method, you will know what row it is. To finish and have the corresponding cell you should do the following:

func capturarPantalla(sender: UIButton) {

    let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
    guard let cell = tableView.cellForRowAtIndexPath(indexPath) else {
        return
    }

    // En este punto ya tienes que "cell" es del tipo UITableViewCell
    // y puedes hacer lo que necesites con dicha celda

}
    
answered by 08.09.2016 / 11:51
source