我想知道是否可以直接从App运行iOS控制中心功能,例如屏幕录制或手电筒?如果有,怎么样?
答案 0 :(得分:1)
使用以下代码来使用手电筒,
func flashlight() {
let flashLight: AVCaptureDevice? = AVCaptureDevice.default(for: .video)
if flashLight?.isTorchAvailable() && flashLight?.isTorchModeSupported(.on) {
let success: Bool? = try? flashLight?.lockForConfiguration()
if success ?? false {
if flashLight?.isTorchActive() != nil {
flashLight?.torchMode = .off
}
else {
flashLight?.torchMode = .on
}
flashLight?.unlockForConfiguration()
}
}
}
从iOS 9开始,屏幕录制看起来像ReplayKit可以大大简化这一点。
func startRecording(_ sender: UIBarButtonItem, _ r: RPScreenRecorder) {
r.startRecording(handler: { (error: Error?) -> Void in
if error == nil { // Recording has started
sender.title = "Stop"
} else {
// Handle error
print(error?.localizedDescription ?? "Unknown error")
}
})
}
func stopRecording(_ sender: UIBarButtonItem, _ r: RPScreenRecorder) {
r.stopRecording( handler: { previewViewController, error in
sender.title = "Record"
if let pvc = previewViewController {
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
pvc.modalPresentationStyle = UIModalPresentationStyle.popover
pvc.popoverPresentationController?.sourceRect = CGRect.zero
pvc.popoverPresentationController?.sourceView = self.view
}
pvc.previewControllerDelegate = self
self.present(pvc, animated: true, completion: nil)
}
else if let error = error {
print(error.localizedDescription)
}
})
}
// MARK: RPPreviewViewControllerDelegate
func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
previewController.dismiss(animated: true, completion: nil)
}
有关详情,请访问此链接:https://developer.apple.com/reference/replaykit