浅写一下iOS录屏开发~ 搬砖人的自我记录
生活随笔
收集整理的這篇文章主要介紹了
浅写一下iOS录屏开发~ 搬砖人的自我记录
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
跟昨天推文一樣,iOS的錄屏功能,知識(shí)點(diǎn)就不寫(xiě)了,用大白話淺寫(xiě)一下開(kāi)發(fā)邏輯 ~
錄屏模塊使用邏輯:
實(shí)際開(kāi)發(fā)應(yīng)用
一. 初始化錄屏
為了能在剛進(jìn)到頁(yè)面的時(shí)候就彈出權(quán)限提示,所以在視圖出現(xiàn)的時(shí)候就初始化錄屏模塊
二. 開(kāi)始錄屏
- (void)startScreenRecord:(void(^)(void))successRecord {RPScreenRecorder *recorder = [RPScreenRecorder sharedRecorder];recorder.microphoneEnabled = YES;// 開(kāi)始[recorder startRecordingWithHandler:^(NSError * _Nullable error) {if (error) {// 無(wú)法開(kāi)啟錄屏功能} else {successRecord();}}]; }三. 停止錄屏
- (void)stopScreenRecord {if ([[RPScreenRecorder sharedRecorder] isRecording]) {[[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:nil];} }四. 帶存儲(chǔ)的停止錄屏
- (void)stopScreenRecoderWithSave {if ([RPScreenRecorder sharedRecorder].isRecording) {if (@available(iOS 14.0, *)) {NSDateFormatter *dateformat = [NSDateFormatter new];[dateformat setDateFormat:@"yyyy_MM_dd_HH_mm_ss"];//文件名NSString *fileName = [NSString stringWithFormat:@"record_screen_%@.mp4",[dateformat stringFromDate:[NSDate date]]];NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];NSString *dirPath = [documentDirectory stringByAppendingPathComponent:@"record_screen_video"];if (![[NSFileManager defaultManager] fileExistsAtPath:dirPath]) {[[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];}NSString *filePath = [dirPath stringByAppendingPathComponent:fileName];[[RPScreenRecorder sharedRecorder] stopRecordingWithOutputURL:[NSURL fileURLWithPath:filePath] completionHandler:^(NSError * _Nullable error) {dispatch_async(dispatch_get_main_queue(), ^{// 存儲(chǔ)視頻UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);});}];} else{[[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController *previewViewController, NSError * error){dispatch_async(dispatch_get_main_queue(), ^{if (!error && [previewViewController respondsToSelector:@selector(movieURL)]) {NSURL *videoURL = [previewViewController.movieURL copy];BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([videoURL path]);if (videoURL && compatible) {NSString *moviePath = [videoURL path];if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {// 存儲(chǔ)視頻UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);}} else {// 未成功保存視頻}} else {// 未成功保存視頻}});}];}} else {// 錄屏失敗} }五. 取視頻操作 (帶壓縮操作)
//保存視頻完成之后的回調(diào) - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {if (error) {// 未成功保存視頻} else {//取出這個(gè)視頻PHFetchOptions *options = [[PHFetchOptions alloc] init];options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; //按創(chuàng)建日期獲取PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];PHAsset *phasset = [assetsFetchResults lastObject];if (phasset) {if (phasset.mediaType == PHAssetMediaTypeVideo) {//是視頻文件PHImageManager *manager = [PHImageManager defaultManager];[manager requestAVAssetForVideo:phasset options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {AVURLAsset *urlAsset = (AVURLAsset *)asset;NSURL *videoURL = urlAsset.URL;NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];NSTimeInterval time = [date timeIntervalSince1970] * 1000;NSString *timeString = [NSString stringWithFormat:@"%.0f", time];NSString *fileName = [NSString stringWithFormat:@"%@_%@",@"tmp",timeString];dispatch_async(dispatch_get_main_queue(), ^{NSString *outPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[fileName stringByAppendingString:@".mp4"]];AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];session.outputURL = [NSURL fileURLWithPath:outPath];session.outputFileType = AVFileTypeMPEG4; //壓縮格式[session exportAsynchronouslyWithCompletionHandler:^(void){ // 視頻壓縮工具if (session.status == AVAssetExportSessionStatusCompleted) {dispatch_async(dispatch_get_main_queue(), ^{//視頻已處理好可以對(duì)其進(jìn)行操作NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:outPath]];if (data) {// 可以上傳視頻了}});} else {dispatch_async(dispatch_get_main_queue(), ^{// 未成功保存視頻});}}];});}];} else {// 未成功保存視頻}} else {// 未成功保存視頻}} }寫(xiě)的很全了,基本能直接用了,有啥問(wèn)題找我~
總結(jié)
以上是生活随笔為你收集整理的浅写一下iOS录屏开发~ 搬砖人的自我记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cad图纸导入ai尺寸变了_AI公司导入
- 下一篇: Android软件开发教学视频(转载)