open file /var/mobile/Media/DCIM 相冊中獲取到的視頻地址使用 報錯 視頻文件不存在


從相冊中獲取到的視頻地址 例如  file:///var/mobile/Media/DCIM/100APPLE/IMG_9876.MOV 

后面再使用的時候報錯 視頻文件不存在  那是因為在ios10、11 設備上需要在請求一次權限

    PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
    options.networkAccessAllowed = YES;
    options.version = PHVideoRequestOptionsVersionCurrent;
    options.progressHandler = ^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
        
    };
    //相冊中獲取到的phAsset;
    PHAsset *phAsset; //= self.sset;
    
    [[PHImageManager defaultManager] requestAVAssetForVideo:phAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
        if(([asset isKindOfClass:[AVComposition class]] && ((AVComposition *)asset).tracks.count == 2)){
            //slow motion videos. See Here: https://overflow.buffer.com/2016/02/29/slow-motion-video-ios/
            
            //Output URL of the slow motion file.
            NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
            NSString *tempDir = [root stringByAppendingString:@"/com.sdk.demo/temp"];
            if (![[NSFileManager defaultManager] fileExistsAtPath:tempDir isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:tempDir withIntermediateDirectories:YES attributes:nil error:nil];
            }
            NSString *myPathDocs =  [tempDir stringByAppendingPathComponent:[NSString stringWithFormat:@"mergeSlowMoVideo-%d.mov",arc4random() % 1000]];
            NSURL *url = [NSURL fileURLWithPath:myPathDocs];
            //Begin slow mo video export
            AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
            exporter.outputURL = url;
            exporter.outputFileType = AVFileTypeQuickTimeMovie;
            exporter.shouldOptimizeForNetworkUse = YES;
            
            [exporter exportAsynchronouslyWithCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (exporter.status == AVAssetExportSessionStatusCompleted) {
                        NSURL *URL = exporter.outputURL;
                        AVURLAsset *asset = [AVURLAsset assetWithURL:URL];
                        dispatch_async(dispatch_get_main_queue(), ^{
//                            completion(asset, nil);
                            //拿到有權限的asset
                        });
                    }else {
                        dispatch_async(dispatch_get_main_queue(), ^{
//                            錯誤
//                            completion(nil, exporter.error);
                        });
                    }
                });
            }];
        }else {
            dispatch_async(dispatch_get_main_queue(), ^{
//                completion(asset, nil);
                //拿到有權限的asset
            });
        }
    }];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM