繁体   English   中英

尝试制作一个屏幕快照按钮,单击该按钮会将屏幕快照发送到我的相机胶卷。 Swift 3,Xcode 8,IOS

[英]Trying to make a screenshot button that sends the screenshot to my camera roll, when the button is clicked. 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)
    }
}
}

问题是您已将带有代码的屏幕快照放在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)        
}

在Swift 3.1中

首先,您需要编辑info.plist文件 在此处输入图片说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM