Skip to content

Using ReplayKit to record a screen

   

You can use ReplayKit to record your screen. It's very easy to use, and it's also very easy to use it on the mic. and so on. This time, I saved it as a video, but it can be used for many things, such as combining it with WebRTC.

ReplayKit

import UIKit
import ReplayKit
class ViewController: UIViewController {
let recorder = RPScreenRecorder.shared()
var iconImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
iconImageView.image = UIImage(named: "icon")
iconImageView.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
view.addSubview(iconImageView)
let startRecordButton = UIButton()
startRecordButton.setTitle("Start", for: .normal)
startRecordButton.backgroundColor = .lightGray
startRecordButton.frame = CGRect(x: view.bounds.size.width - 200, y: view.bounds.size.height - 100, width: 60, height: 60)
startRecordButton.addTarget(self, action: #selector(startRecording), for: .touchDown)
view.addSubview(startRecordButton)
let stopRecordButton = UIButton()
stopRecordButton.setTitle("Stop", for: .normal)
stopRecordButton.backgroundColor = .lightGray
stopRecordButton.frame = CGRect(x: view.bounds.size.width - 100, y: view.bounds.size.height - 100, width: 60, height: 60)
stopRecordButton.addTarget(self, action: #selector(startRecording), for: .touchDown)
view.addSubview(stopRecordButton)
recorder.isMicrophoneEnabled = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let point = touch.location(in: self.view)
iconImageView.center = point
}
}
override open func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let point = touch.location(in: self.view)
iconImageView.center = point
}
}
override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let point = touch.location(in: self.view)
iconImageView.center = point
}
}
@objc func startRecording() {
recorder.startRecording { (error) in
if let error = error {
print(error)
}
}
}
@objc func endRecording() {
recorder.stopRecording { (previewVC, error) in
if let previewVC = previewVC {
previewVC.previewControllerDelegate = self
self.present(previewVC, animated: true)
}
if let error = error {
print(error)
}
}
}
}
extension UIViewController: RPPreviewViewControllerDelegate {
public func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
dismiss(animated: true, completion: nil)
}
}

  1. Create a sequential numbered array in Swift
  2. Aligning Views like UICollectionView in SwiftUI
  3. Displaying a Firestore image in SwiftUI
  4. Detecting changes to the SwiftUI Toggle
  5. Creating a multi-line picker in SwiftUI
  6. Don't show the Label in the SwiftUI Picker
  7. Displaying a Picker in SwiftUI