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

Tes*_*one 10 screen recording ios swift

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

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

试过这些

  1. https://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos
  2. https://github.com/alskipp/ASScreenRecorder

但是这些图书馆不会提供高质量的视频.我需要高质量的视频.

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

bio*_*ker 5

从 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代码对我有用:

import ReplayKit

@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)
}
Run Code Online (Sandbox Code Playgroud)


小智 2

查看 ScreenCaptureView,它内置了视频录制支持(请参阅链接)。

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

我相信它尚未使用 OpenGL 子视图进行测试,但假设它有效,您可能可以稍微修改它以包含音频,然后您就可以设置了。

AVCaptureSession 示例

AVCaptureSession 参考

import UIKit
import AVFoundation
class ViewController: UIViewController {
    let captureSession = AVCaptureSession()
    let stillImageOutput = AVCaptureStillImageOutput()
    var error: NSError?
    override func viewDidLoad() {
        super.viewDidLoad()
        let devices = AVCaptureDevice.devices().filter{ $0.hasMediaType(AVMediaTypeVideo) && $0.position == AVCaptureDevicePosition.Back }
        if let captureDevice = devices.first as? AVCaptureDevice  {

            captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &error))
            captureSession.sessionPreset = AVCaptureSessionPresetPhoto
            captureSession.startRunning()
            stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
            if captureSession.canAddOutput(stillImageOutput) {
                captureSession.addOutput(stillImageOutput)
            }
            if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
                previewLayer.bounds = view.bounds
                previewLayer.position = CGPointMake(view.bounds.midX, view.bounds.midY)
                previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                let cameraPreview = UIView(frame: CGRectMake(0.0, 0.0, view.bounds.size.width, view.bounds.size.height))
                cameraPreview.layer.addSublayer(previewLayer)
                cameraPreview.addGestureRecognizer(UITapGestureRecognizer(target: self, action:"saveToCamera:"))
                view.addSubview(cameraPreview)
            }
        }
    }
    func saveToCamera(sender: UITapGestureRecognizer) {
        if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
            stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
                (imageDataSampleBuffer, error) -> Void in
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
                 UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData), nil, nil, nil)
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我不想录制相机,而是想录制屏幕。 (4认同)