【发布时间】: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 秒,但仍然错误