您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 自帶錄屏框架 ReplayKit 的使用與踩坑總結

編輯:iOS 開發綜合
前幾個月第一次做 iOS 錄屏功能,上網查了 ReplayKit 的相關資料並集成到項目中,初步實現了錄屏需求。但後續測試發現偶爾會出現黑屏、無法保存到系統相冊等問題,後來調整了調用順序才得以解決。目前基本穩定,但尚未正式上線,這裡把代碼整理出來,歡迎指正。

ReplayKit 是 iOS 9 之後才有的框架,因此僅支持 iOS 9 及以上系統錄屏。

開始錄屏:

 - (void)StartRecoder { dispatch_async(dispatch_get_main_queue(), ^{ if ([[RPScreenRecorder sharedRecorder] isAvailable] && [self isSystemVersionOk]) { NSLog(@"支持ReplayKit錄制"); RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder; recorder.delegate = self; recorder.microphoneEnabled = YES; recorder.cameraEnabled = YES; [recorder startRecordingWithHandler:^(NSError * _Nullable error) { recorder.microphoneEnabled = YES; if (error) { NSLog(@"========%@",error.description); _DecoderButton.selected = NO; } else { if (recorder.recording) { _DecoderButton.selected = YES; IsDecoderScreen = 1; } } }]; } else { [self showAlert:@"設備不支持錄制" andMessage:@"升級ios系統"]; return; } }); }

結束錄屏並彈出預覽:

 - (void)stopDecoder { [[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController *previewViewController, NSError * error){ _RPPreview = previewViewController; if (error) { NSLog(@"這裡關閉有誤%@",error.description); } else { [_RPPreview setPreviewControllerDelegate:self]; _DecoderButton.selected = NO; IsDecoderScreen = 0; [self showVideoPreviewController:_RPPreview withAnimation:YES]; } }]; }

顯示與關閉視頻預覽頁面(注意 UI 操作需切回主線程):

 - (void)showVideoPreviewController:(RPPreviewViewController *)previewController withAnimation:(BOOL)animation { dispatch_async(dispatch_get_main_queue(), ^{ [self.view addSubview:previewController.view]; [self addChildViewController:previewController]; }); } - (void)hideVideoPreviewController:(RPPreviewViewController *)previewController withAnimation:(BOOL)animation { dispatch_async(dispatch_get_main_queue(), ^{ [previewController.view removeFromSuperview]; [previewController removeFromParentViewController]; }); }

預覽頁關閉與保存/分享的回調:

 #pragma mark -RPPreviewViewControllerDelegate - (void)previewControllerDidFinish:(RPPreviewViewController *)previewController { if (isSave == 1) { // 延時執行,避免保存不到系統相冊 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self hideVideoPreviewController:_RPPreview withAnimation:YES]; }); isSave = 0; } } - (void)previewController:(RPPreviewViewController *)previewController didFinishWithActivityTypes:(NSSet <NSString *> *)activityTypes { if ([activityTypes containsObject:@"com.apple.UIKit.activity.SaveToCameraRoll"]) { isSave = 1; } if ([activityTypes containsObject:@"com.apple.UIKit.activity.CopyToPasteboard"]) { dispatch_async(dispatch_get_main_queue(), ^{ [self showAlert:@"復制成功" andMessage:@"已經復制到粘貼板"]; }); } }

RPScreenRecorderDelegate 相關回調:

 - (void)screenRecorderDidChangeAvailability:(RPScreenRecorder *)screenRecorder { NSLog(@"delegate ======%@",screenRecorder); } - (void)screenRecorder:(RPScreenRecorder *)screenRecorder didStopRecordingWithError:(NSError *)error previewViewController:(nullable RPPreviewViewController *)previewViewController { [_RPPreview setPreviewControllerDelegate:self]; _DecoderButton.selected = NO; IsDecoderScreen = 0; [self showVideoPreviewController:_RPPreview withAnimation:YES]; }

判斷系統版本是否支持 ReplayKit:

 - (BOOL)isSystemVersionOk { if ([[UIDevice currentDevice].systemVersion floatValue] < 9.0) { return NO; } else { return YES; } }

實踐中發現,保存到相冊時若不加延時偶爾會失敗,加入約定的延時執行後明顯改善。這也提醒我們,ReplayKit 的相關 UI 回調要確保在主線程處理,並注意調用順序,才能避免黑屏與保存異常。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved