1

My hope is to save a screenshot successfully, but until now, my code is saving the screen twice and I do not know why.

I have the following:

class finalPostal: UIViewController{

    var gesture = UILongPressGestureRecognizer()


  override func viewWillAppear(animated: Bool) {

        gesture = UILongPressGestureRecognizer(target: self, action: "screenShotMethod")

        gesture.minimumPressDuration = 1.5

        view.addGestureRecognizer(gesture)
    }


   func screenShotMethod() {


        let layer = UIApplication.sharedApplication().keyWindow!.layer
        let scale = UIScreen.mainScreen().scale
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);

        layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let screenshot = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

        let alertSaved = UIAlertController(title: "Caputra guardada", message: "", preferredStyle: .ActionSheet)

        alertSaved.addAction(UIAlertAction(title: "Ok", style: .Default, handler: {action in

            self.view.removeGestureRecognizer(self.gesture)

        }))

        presentViewController(alertSaved, animated: true, completion: nil)

    }
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
FactorJose
  • 471
  • 2
  • 6
  • 14

1 Answers1

1

You have to check the state of gesture and take the screenshot only when the state is equals to

UIGestureRecognizerStateEnded

In your screenShotMethod method

chiarotto.alessandro
  • 1,491
  • 1
  • 13
  • 31