2017-02-15 47 views
2

Swift 3,Xcode 8,IOS:試圖製作一個屏幕快照按鈕,將屏幕截圖發送到我的相機膠捲上,然後單擊按鈕。 Swift 3,Xcode 8,IOS

我似乎無法弄清楚我做錯了什麼,沒有錯誤出現,但是當我點擊我的應用程序中的按鈕,沒有任何反應,我的模擬器相機膠捲中沒有任何東西被保存。

這是我在視圖控制器的按鈕來完成:

import UIKit 
class ViewController: ViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

@IBAction func buttonAction(_ sender: UIButton) { 
    func captureScreen() { 

     UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale) 
     view.layer.render(in: UIGraphicsGetCurrentContext()!) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil) 
    } 
} 
} 

回答

2

問題是你已經把截圖以代碼的buttonAction方法名captureScreen的嵌套函數裏面,從來沒有叫那個方法,沒有必要添加嵌套方法。因此,只需刪除該功能並將截屏代碼直接放入按鈕操作方法中即可。所以用這個替換你的按鈕動作。

@IBAction func buttonAction(_ sender: UIButton) { 

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale) 
    view.layer.render(in: UIGraphicsGetCurrentContext()!) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)   
} 
+0

「這個程序已經崩潰,因爲它試圖在沒有使用說明的情況下訪問隱私敏感數據。該應用程序的Info.plist m ust包含一個帶有字符串值的NSPhotoLibraryUsageDescription項,向用戶解釋應用程序如何使用此數據。 (lldb)「 – DanielG

+0

@DanielG檢查這個答案爲http://stackoverflow.com/a/40012626/6433023,並添加到您的隱私照片庫info.plist –

+0

是的,謝謝!:) – DanielG

1

在雨燕3.1

首先,你需要編輯您的Info.plist文件 enter image description here

import Photos   

然後添加的UIButton:

@IBOutlet weak var shutterButton: UIButton! 



@IBAction func shotAction(_ sender: Any) { 
    guard shutterButton.isEnabled else { 
     return 
    } 

    let takeScreenshotBlock = { 
     //Render and capture an UIView named view: 
     self.shutterButton.isHidden = true 
     UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0.0) 
     self.view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true) 
     if let image = UIGraphicsGetImageFromCurrentImageContext() { 
      UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 
     } 
     UIGraphicsEndImageContext(); 
     self.shutterButton.isHidden = false 


     DispatchQueue.main.async { 
      // briefly flash the screen 
      let flashOverlay = UIView(frame: self.sceneView.frame) 
      flashOverlay.backgroundColor = UIColor.white 
      self.sceneView.addSubview(flashOverlay) 
      UIView.animate(withDuration: 0.25, animations: { 
       flashOverlay.alpha = 0.0 
      }, completion: { _ in 
       flashOverlay.removeFromSuperview() 
      }) 
     } 
    } 
    switch PHPhotoLibrary.authorizationStatus() { 
    case .authorized: 
     takeScreenshotBlock() 
    case .restricted, .denied: 
     let alertController = UIAlertController(title: "Photo access denied", message: "Please enable Photos Library access for this appliction in Settings > Privacy.", preferredStyle: UIAlertControllerStyle.alert) 
     let actionOK = UIAlertAction(title: "OK", style: .cancel, handler: nil) 
     alertController.addAction(actionOK) 
     present(alertController, animated: true, completion: nil) 
    case .notDetermined: 
     PHPhotoLibrary.requestAuthorization({ (status) in 
      if status == .authorized { 
       takeScreenshotBlock() 
      } 
     }) 
    } 
}