如何以编程方式记录IOS屏幕

时间:2016-07-12 07:46:17

标签: ios swift screen recording

有没有办法以编程方式记录IOS屏幕。表示您正在执行的任何活动,例如单击按钮,滚动表格视图。

即使播放的视频会与其他活动一起再次播放?

试过这些

  1. https://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos
  2. https://github.com/alskipp/ASScreenRecorder
  3. 但是这些图书馆不能提供高质量的视频。我需要高质量的视频。

    问题是,当我在捕捉屏幕时在后台播放视频时,它不会显示流畅的视频。它显示像一帧视频,然后在3-4秒后第二帧,依此类推。视频质量也不好模糊

4 个答案:

答案 0 :(得分:3)

从iOS 9开始,看起来ReplayKit可以大大简化这一点。

https://developer.apple.com/reference/replaykit

https://code.tutsplus.com/tutorials/ios-9-an-introduction-to-replaykit--cms-25458

更新:由于iOS 11内置了屏幕录像机,因此可能不太相关,但以下 Swift 3 代码对我有用:

    @IBAction func toggleRecording(_ sender: UIBarButtonItem) {
    let r = RPScreenRecorder.shared()

    guard r.isAvailable else {
        print("ReplayKit unavailable")
        return
    }

    if r.isRecording {
        self.stopRecording(sender, r)

    }
    else {
        self.startRecording(sender, r)
    }
}

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)
}

答案 1 :(得分:2)

查看ScreenCaptureView,内置视频录制支持(参见链接)。

这样做是为了将UIView的内容保存到UIImage。作者建议您可以通过AVCaptureSession传递帧来保存正在使用的应用程序的视频。

我相信它还没有经过OpenGL子视图的测试,但假设它有效,你可以稍微修改它以包含音频然后你就可以了。

AVCaptureSession示例

AVCaptureSession Reference

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div class="draggableTemplate ui-draggable ui-draggable-handle ui-resizable">
        Some Text ...
        <div class="ui-resizable-handle ui-resizable-sw" >Remove</div>
        <div class="ui-resizable-handle ui-resizable-n" >Remove</div>
    </div>
    <div class="draggableTemplate ui-draggable ui-draggable-handle ui-resizable">
        <img width="114" height="93" alt="" src="/Cats/cat.jpg" />
        <div class="ui-resizable-handle ui-resizable-sw" >Remove</div>
        <div class="ui-resizable-handle ui-resizable-n" >Remove</div>
    </div>

<button id="remove">Remove</button>

答案 2 :(得分:1)

ReplayKit可用,虽然您不允许访问结果视频,但到目前为止,我发现的唯一方法是制作一些屏幕截图(将它们存储在图像数组中),然后将这些图像转换为视频虽然从性能角度来看并不是非常有效,但是当你不需要30/60 fps的屏幕录制时可能会有效,并且可能没有6-20 pfs。这是the full example

答案 3 :(得分:0)

您可以使用此库记录视图:在目标C中编写的GitHub上可用的screen-cap-view

**And to use it in swift:**

--> Drag and drop the .m and .h files in your xcode project.

--> Make a header file and import the this file in that : *#import "IAScreenCaptureView.h"*

--> Then give a View this class from the PropertyInspector and then make a IBOutlet for that view . Something like this:
*@IBOutlet weak var contentView: IAScreenCaptureView!*

--> Then Finally just simply start and stop the recording of the view where ever and when ever you want and for that the code will be like this :

For Starting the Recording : *contentView.startRecording()*
For Stoping the Recording : *contentView.stopRecording()*


//Hope this helps.Happy coding.  \o/ , ¯\_(ツ)_/¯ ,(╯°□°)╯︵ ┻━┻