iOS录屏框架ReplayKit的应用总结
ReplayKit是iOS自带的一个屏幕录制的框架,其支持应用程序对当前应用内页面进行录屏,并将最终的视频保存到系统相册中。ReplayKit在iOS 9之后引入,其接口简介,可以非常方便的为应用添加录屏功能。需要注意,在某些iOS 12系统上,开启录屏可能会失败(通常需要重启设备解决)。
在ReplayKit框架中,有两个非常重要的类,分别是RPScreenRecorder类与RPPreviewViewController类。RPScreenRecorder是录屏核心功能类,RPPreviewViewController是录屏结束后的预览控制器类。
下面,列举了一个简单的录屏示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| @interface ViewController () <RPScreenRecorderDelegate, RPPreviewViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad { self.view.backgroundColor = [UIColor whiteColor]; [super viewDidLoad]; NSLog(@"%d", [RPScreenRecorder sharedRecorder].available); [RPScreenRecorder sharedRecorder].delegate = self; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; [btn setTitle:@"Start" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside]; btn.frame = CGRectMake(100, 100, 100, 30); [self.view addSubview:btn]; UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem]; [btn2 setTitle:@"End" forState:UIControlStateNormal]; [btn2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside]; btn2.frame = CGRectMake(100, 150, 100, 30); [self.view addSubview:btn2]; }
- (void)stop { [[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController * _Nullable previewViewController, NSError * _Nullable error) { NSLog(@"stopRecordingWithHandler"); if (!error) { previewViewController.previewControllerDelegate = self; [self presentViewController:previewViewController animated:YES completion:nil]; } }]; }
- (void)start { [[RPScreenRecorder sharedRecorder] startRecordingWithHandler:^(NSError * _Nullable error) { NSLog(@"startRecordingWithHandlerError:%@", error); }]; }
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { self.view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1]; }
- (void)screenRecorder:(RPScreenRecorder *)screenRecorder didStopRecordingWithPreviewViewController:(nullable RPPreviewViewController *)previewViewController error:(nullable NSError *)error { NSLog(@"didStopRecordingWithPreviewViewControllerError:%@", error); }
- (void)screenRecorderDidChangeAvailability:(RPScreenRecorder *)screenRecorder { NSLog(@"screenRecorderDidChangeAvailability:%d", screenRecorder.available); }
- (void)previewControllerDidFinish:(RPPreviewViewController *)previewController { [previewController dismissViewControllerAnimated:YES completion:nil]; }
- (void)previewController:(RPPreviewViewController *)previewController didFinishWithActivityTypes:(NSSet <NSString *> *)activityTypes { NSLog(@"didFinishWithActivityTypes %@", activityTypes); }
@end
|
其中,RPScreenRecorder类中提供了丰富的接口可以使用,列举如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| @interface RPScreenRecorder : NSObject
+ (RPScreenRecorder *)sharedRecorder;
- (void)startRecordingWithMicrophoneEnabled:(BOOL)microphoneEnabled handler:(nullable void(^)(NSError * _Nullable error))handler;
- (void)startRecordingWithHandler:(nullable void(^)(NSError * _Nullable error))handler;
- (void)stopRecordingWithHandler:(nullable void(^)(RPPreviewViewController * _Nullable previewViewController, NSError * _Nullable error))handler;
- (void)discardRecordingWithHandler:(void(^)(void))handler;
- (void)startCaptureWithHandler:(nullable void(^)(CMSampleBufferRef sampleBuffer, RPSampleBufferType bufferType, NSError * _Nullable error))captureHandler completionHandler:(nullable void(^)(NSError * _Nullable error))completionHandler;
- (void)stopCaptureWithHandler:(nullable void(^)(NSError * _Nullable error))handler;
@property (nonatomic, weak, nullable) id<RPScreenRecorderDelegate> delegate;
@property (nonatomic, readonly, getter=isAvailable) BOOL available;
@property (nonatomic, readonly, getter=isRecording) BOOL recording;
@property (nonatomic, getter=isMicrophoneEnabled) BOOL microphoneEnabled;
@property (nonatomic, getter=isCameraEnabled) BOOL cameraEnabled;
@property (nonatomic) RPCameraPosition cameraPosition;
@property (nonatomic, readonly, nullable) UIView *cameraPreviewView; @end
|
在录屏的时候,也支持调用系统的麦克风和摄像头共同完成录制。RPScreenRecorderDelegate协议中定义了一些回调方法,如下:
1 2 3 4 5 6
| - (void)screenRecorder:(RPScreenRecorder *)screenRecorder didStopRecordingWithError:(NSError *)error previewViewController:(nullable RPPreviewViewController *)previewViewController;
- (void)screenRecorder:(RPScreenRecorder *)screenRecorder didStopRecordingWithPreviewViewController:(nullable RPPreviewViewController *)previewViewController error:(nullable NSError *)error;
- (void)screenRecorderDidChangeAvailability:(RPScreenRecorder *)screenRecorder;
|
RPPreviewViewController类是视频预览控制器类,这个控制器没有暴露太多的属性给开发者使用,其中预览模式可以选择分享模式或编辑模式,除此之外,其中还提供了一个代理协议给开发者进行使用,用来对用户的操作进行处理,如下:
1 2 3 4 5 6 7
| @protocol RPPreviewViewControllerDelegate <NSObject> @optional
- (void)previewControllerDidFinish:(RPPreviewViewController *)previewController;
- (void)previewController:(RPPreviewViewController *)previewController didFinishWithActivityTypes:(NSSet <NSString *> *)activityTypes; @end
|