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

问题描述 投票:9回答:4

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

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

试过这些

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

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

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

ios swift screen recording
4个回答
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)
}

2
投票

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

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

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

AVCaptureSession示例

AVCaptureSession Reference

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

1
投票

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


0
投票

您可以使用此库来记录视图:在Objective 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/ , ¯\_(ツ)_/¯ ,(╯°□°)╯︵ ┻━┻
© www.soinside.com 2019 - 2024. All rights reserved.