iOS架构音视频

iOS 录屏功能

2019-10-31  本文已影响0人  蒋伟_iOS

使用 ReplayKit类 可以进行屏幕录制

一、检测设备

首先要看设备是否支持录屏

dispatch_async(dispatch_get_main_queue(), ^{
        RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;
        if ([recorder isAvailable] && [YX_ReplayManager systemVersionIsAvailable]) {
            if ([YX_ReplayManager systemVersionIsRisk]) {
                UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的系统版本小于10.2,如果录屏存在问题请使用版本较高的系统" preferredStyle:(UIAlertControllerStyleAlert)];
                UIAlertAction *sureAct = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    [self REC_PermissionsAlert];
                }];
                [alertVC addAction:sureAct];
                [self presentViewController:alertVC animated:YES completion:nil];
            } else {
                [self REC_PermissionsAlert];
            }
        } else {
            [self alertWindowWithMessage:@"您的手机系统版本低于9.0,无法进行屏幕录制操作,请升级手机系统" actionName:nil];
        }
    });

二、开始录屏

使用框架的方法进行录屏,注意要在主线程中进行

[recorder startRecordingWithHandler:^(NSError * _Nullable error) {
                if (!error) {
                    weakSelf.REC_StartOK = YES;
                    if (!weakSelf.canRecord) {
                        [weakSelf stopREC_WithSave:NO alert:NO tryAgain:YES];
                    }
                    NSLog(@"===启动成功");
                }
            }];

三、结束录屏

点结束录屏后会回掉

[[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController *previewViewController, NSError *  error){
       _RPPreview = previewViewController;
       if (error) {
           NSLog(@"这里关闭有误%@",error.description);
           [self showAlert:@"结束录制失败" andMessage:@"本次保存失败!请重新录制"];
           startRecordScreenBtn.backgroundColor = [UIColor greenColor];
           [startRecordScreenBtn setTitle:@"录屏" forState:UIControlStateNormal];
       } else {
           [_RPPreview setPreviewControllerDelegate:self];
           startRecordScreenBtn.backgroundColor = [UIColor greenColor];
           [startRecordScreenBtn setTitle:@"录屏" forState:UIControlStateNormal];
           IsDecoderScreen = 0;
           //在结束录屏时显示预览画面
           [weakSelf showVideoPreviewController:_RPPreview withAnimation:YES];
       }
   }];

四、保存

保存到指定路径

- (void)showVideoPreviewController:(RPPreviewViewController *)previewController withAnimation:(BOOL)animation {
    if (previewController==nil) {
        return;
    }
    __weak typeof (self) weakSelf = self;
    //UI需要放到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        CGRect rect = previewController.view.frame;
        if (animation) {
            rect.origin.x += rect.size.width;
            previewController.view.frame = rect;
            rect.origin.x -= rect.size.width;
            [UIView animateWithDuration:0.3 animations:^(){
                previewController.view.frame = rect;
            } completion:^(BOOL finished){
            }];
        } else {
            previewController.view.frame = rect;
        }
        
        [weakSelf.view addSubview:previewController.view];
        [weakSelf addChildViewController:previewController];
    });
}

这时候录屏文件就进入了沙盒里面,如果要保存到相册自行实现。

上一篇下一篇

猜你喜欢

热点阅读