将屏幕截图保存两次而不是一次时出错

时间:2015-12-14 17:30:24

标签: ios swift

我希望能够成功保存屏幕截图,但直到现在,我的代码正在保存屏幕两次,我不知道原因。

我有以下内容:

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)

    }

1 个答案:

答案 0 :(得分:1)

您必须检查手势的状态,并仅在状态等于

时才截取屏幕截图
  

UIGestureRecognizerStateEnded

在你的screenShotMethod方法