Estoy haciendo una app en swift
, en la que necesito poder hacer una captura de pantalla de una celda concreta de una tabla desde un botón.
Tengo este código:
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)
}
Pero me da este error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell capturarPantalla:]: unrecognized selector sent to instance 0x13895a5b0'
Como puedo pasar un parametro en un selector
EDITADO
He añadido la solucion de @mhergon pero ahora tengo un error aqui, este error no pasa en todas las celdas, solo en algunas:
btnCaptura = cell.viewWithTag(20) as! UIButton
PONE TAG 99 PERO ES PORQUE LO HE CAMBIADO GLOBALMENTE POR SI ACASO. EN LA CELDA DEL TABLE VIEW ESTA TAMBIEN A 99
guard ... else
que para eso está y comprobar que el botón no esnil