0

我想知道是否可以直接从应用程序运行 iOS 控制中心功能,例如屏幕录制或手电筒?如果是,如何?

4

1 回答 1

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

于 2017-11-09T07:39:02.243 回答