尝试制作一个屏幕截图按钮,当按钮被点击时,该按钮会将屏幕截图发送到我的相机卷。Swift 3、Xcode 8、IOS

尝试制作一个屏幕截图按钮,当按钮被点击时,该按钮会将屏幕截图发送到我的相机卷。Swift 3、Xcode 8、IOS,ios,swift3,save,screenshot,Ios,Swift3,Save,Screenshot,Swift 3、Xcode 8、IOS: 我似乎不知道我做错了什么,没有显示错误,但当我点击应用程序中的按钮时,什么都没有发生,也没有保存在我的模拟器摄像机卷中 这是我为视图控制器中的按钮所做的: import UIKit class ViewController: ViewController { override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning()

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)
    }
}
}

问题是,您已经将截图代码放在
按钮操作
方法名称
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)        
}
在Swift 3.1中

首先,您需要编辑info.plist文件

然后添加UI按钮:

@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()
            }
        })
    }
}

“此应用程序已崩溃,因为它试图在没有使用说明的情况下访问隐私敏感数据。此应用程序的Info.plist必须包含一个NSPhotoLibraryUsageDescription密钥,该密钥带有一个字符串值,用于向用户解释应用程序如何使用此数据。(lldb)”@DanielG检查此答案,并在您的Info.plisties中添加隐私照片库,谢谢!:)@DanielG欢迎伴侣:)@NiravD+1非常感谢你,这很有帮助
@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()
            }
        })
    }
}