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
PUT TAG 99 BUT IT IS BECAUSE I HAVE CHANGED IT GLOBALLY BY MEASURE. ON THE TABLE VIEW CELL IS ALSO 99