【问题标题】:Error saving a screenshot twice instead of once [duplicate]两次而不是一次保存屏幕截图时出错[重复]
【发布时间】:2015-12-14 17:30:23
【问题描述】:

我希望能成功保存截图,但到现在为止,我的代码保存了两次屏幕,我不知道为什么。

我有以下几点:

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)

    }

【问题讨论】:

  • 请修正您的代码格式
  • 你要等多久。根据按下的持续时间,它可能会被多次调用
  • 在代码上设置断点,看看该方法是否被命中两次。如果是,请使用 bool 变量以避免多次点击
  • 你可以创建一个函数来确保它没有被经常调用。
  • 我尝试将持续时间更改为 3 秒,但仍然错误

标签: ios swift


【解决方案1】:

你必须检查手势的状态,只有当状态等于时才能截图

UIGestureRecognizerStateEnded

在你的 screenShotMethod 方法中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    相关资源
    最近更新 更多