iOS的录屏功能其实没什么好说的,因为网上的教程很多,但是网上的Demo无一例外几乎都有一个bug,那就是iPad上会出现闪退,这也体现了国内的教程文档的一个特点,就是抄袭,教程几乎千篇一律,bug也是一摸一样,经过多次测试,终于找着了解决方案。
废话少说,代码献上。
//开始录屏 extern "C" void startRecord() { NSLog(@"开始录屏"); if ([RPScreenRecorder sharedRecorder].isRecording) { return; } // [[RPScreenRecorder sharedRecorder] startRecordingWithHandler:^(NSError * _Nullable error) { //// UnitySendMessage("Buttons", "RecordCallback", [RPScreenRecorder sharedRecorder].isRecording ? "true" : "false"); // }]; if (isSupportReplayKit()) { [[RPScreenRecorder sharedRecorder] startRecordingWithMicrophoneEnabled:YES handler:^(NSError * _Nullable error) { NSLog(@"steve :%@", error); //处理发生的错误,如设用户权限原因无法开始录制等 }]; } } //结束录屏 extern "C" void stopRecord() { UIButton *sender = [UIButton buttonWithType:(UIButtonTypeRoundedRect)]; NSLog(@"停止录屏"); if ([RPScreenRecorder sharedRecorder].isRecording) { NSLog(@"正在录屏中"); [[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController * _Nullable previewViewController, NSError * _Nullable error) { if (previewViewController) { if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){ previewViewController.previewControllerDelegate = previewDelegate; previewViewController.preferredContentSize = CGSizeMake(1536, 2048); previewViewController.modalPresentationStyle = UIModalPresentationPopover; UIPopoverPresentationController *popoverpresentationController = previewViewController.popoverPresentationController; [GetAppController().window.rootViewController presentViewController:previewViewController animated:YES completion:^{ UnityPause(YES); }]; popoverpresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp; popoverpresentationController.sourceRect = sender.bounds; popoverpresentationController.sourceView = sender; }else{ previewViewController.previewControllerDelegate = previewDelegate; [GetAppController().window.rootViewController presentViewController:previewViewController animated:YES completion:^{ UnityPause(YES); }]; } UnitySendMessage("Canvas/Root/gamepanel(Clone)", "HideStopRecordButton", @"".UTF8String); } }]; } }