iOS录制屏幕之ReplayKit-iOS 10/tvOS 10

tvOS 10的开始支持了ReplayKit.(但仅仅有屏幕录制功能 )

新增属性

@property (nonatomic, getter=isCameraEnabled) BOOL cameraEnabled API_AVAILABLE(ios(10.0)) API_UNAVAILABLE(tvos);

@property (nonatomic, readonly, nullable) UIView *cameraPreviewView API_AVAILABLE(ios(10.0)) API_UNAVAILABLE(tvos);

说明:因为tvOS,也就是苹果那个机顶盒,不带麦克风,所以和麦克风有关的属性不能在tvOS上用

cameraPreviewView在iOS 10以上支持,可以将前置摄像头的内容展示在这个View上.

新增方法

- (void)startRecordingWithHandler:(nullable void(^)(NSError * _Nullable error))handler

开启录制的调用方式变了.iOS 9的开始录制方法只能在录制之前确定是否开启麦克风支持,并且后面不能改变.但是iOS 10的这个方法调用方式就不太一样了.

if (recorder.isAvailable) {
    if (!recorder.isRecording) {
		// 开启摄像头
        recorder.cameraEnabled = YES;
        // 开启录音
        recorder.microphoneEnabled = YES;
        [recorder startRecordingWithHandler:^(NSError * _Nullable error) {
            	// 开始录制代码块
        }];
    }
}

注:iOS 10的开始录制方法需要在info.plist中添加NSCameraUsageDescription,不然会崩溃,然而和麦克风有关的倒是没事儿(PS:苹果这样定义挺坑的)

摄像头的显示

因为主要代码是和开始录制有关的.所以我就直接写和上面的相比有变化的地方了.

[recorder startRecordingWithHandler:^(NSError * _Nullable error) {
    if (!error) {
        dispatch_async(dispatch_get_main_queue(), ^{
        	// 注:开始录制回调是异步的
            UIView *cameraPreviewView = recorder.cameraPreviewView;
            [self.view addSubview:cameraPreviewView];
            cameraPreviewView.translatesAutoresizingMaskIntoConstraints = NO;
            [cameraPreviewView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
            [cameraPreviewView.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;
            [cameraPreviewView.widthAnchor constraintEqualToConstant:100].active = YES;
            [cameraPreviewView.heightAnchor constraintEqualToConstant:100].active = YES;
        });
    }
}];

PS:屏幕录制的内容也包括了这个cameraPreviewView.(如果当前屏幕上显示了cameraPreviewView),

发布了268 篇原创文章 · 获赞 59 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/103649191
10