日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

浅写一下iOS录屏开发~ 搬砖人的自我记录

發(fā)布時(shí)間:2023/12/29 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅写一下iOS录屏开发~ 搬砖人的自我记录 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

跟昨天推文一樣,iOS的錄屏功能,知識(shí)點(diǎn)就不寫(xiě)了,用大白話淺寫(xiě)一下開(kāi)發(fā)邏輯 ~


錄屏模塊使用邏輯:

  • 初始化錄屏
  • 開(kāi)始用就完事了
  • 實(shí)際開(kāi)發(fā)應(yīng)用

    一. 初始化錄屏
    為了能在剛進(jìn)到頁(yè)面的時(shí)候就彈出權(quán)限提示,所以在視圖出現(xiàn)的時(shí)候就初始化錄屏模塊

    - (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];[self initScreenRecord]; } #pragma mark - 錄屏 - (void)initScreenRecord {// 相機(jī)權(quán)限AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {//無(wú)權(quán)限 需要跳轉(zhuǎn)到設(shè)置里開(kāi)啟權(quán)限NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];if ([[UIApplication sharedApplication] canOpenURL:url]) {[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];}} else {// 錄屏if (![[RPScreenRecorder sharedRecorder] isAvailable]) {// 無(wú)法開(kāi)啟錄屏功能} else {NSLog(@"開(kāi)始錄屏了");}} }

    二. 開(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)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。