LoginSignup
1
3

【iOS】画面全体を録画する

Last updated at Posted at 2023-05-24

はじめに

特定のWindowやWindowSceneを画面録画したくて、CVPixelBufferに変換するなどして頑張ってた。
複数Windowの描画や、画面の回転(orientation)を考慮しようとすると割と大変だった。
これは成果物

一方で、スクリーン全体を録画したいのなら、ReplayKitを使用すると簡単に行えることが分かったので、そのやり方をメモに残します。

本編

準備

importしてRPScreenRecorderのsharedインスタンスを取得

import ReplayKit

let recorder = RPScreenRecorder.shared()

録画開始

guard recorder.isAvailable,
      !recorder.isRecording else {
    return
}

recorder.startRecording()

上記のメソッドを呼ぶと、画面の取り込みをしても良いかのダイアログが表示されるようになっている。
一度同意しても、startRecordingを呼ぶたびに毎回表示されるようだ。

A.PNG

録画終了

outputURLを指定してstopRecordingを呼び出すと、動画ファイルが保存される。

let outputURL = FileManager.default.temporaryDirectory.appending(components: "output.mp4")

recorder.stopRecording(withOutput: outputURL) { error in
    print(error.debugDescription)
}

おわりに

owari

画面全体を録画したいなら、はじめにで示した、UIView->CVPixelBufferの方法より、明らかにパフォーマンスが良い。
同時にアプリ内音声やマイクも使用可能である。

IOKitのIOSurfaceを使用すれば、RepleayKitを使わずとも同じくらいのパフォーマンスで録画できると思うが、プライベートメソッドを触ることになるので、AppStoreには出せないかもしれない。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3