2015-12-14 33 views
1

我的希望是成功保存屏幕截圖,但直到現在,我的代碼保存了兩次屏幕,我不知道爲什麼。保存截圖兩次而不是一次的錯誤

我有以下幾點:

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) 

    } 
+0

請修復您的公司的格式de –

+1

你需要多長時間?它可能會根據新聞的持續時間多次調用 – 2015-12-14 17:33:19

+0

在代碼上放置一個斷點以查看該方法是否被命中兩次。如果是,使用bool變量來避免多次匹配 –

回答

1

你必須檢查操作的狀態,並採取截圖的狀態是,只有當等於

UIGestureRecognizerStateEnded

在你screenShotMethod方法