尝试制作一个屏幕截图按钮,在单击该按钮时将屏幕截图发送到我的相机胶卷。 Swift3,Xcode8,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
Swift3,Xcode8,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)
}
在Swift3.1
首先您需要编辑 info.plist 文件
import Photos
然后添加 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()
}
})
}
}
Swift3,Xcode8,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)
}
在Swift3.1
首先您需要编辑 info.plist 文件
import Photos
然后添加 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()
}
})
}
}