How to get NSData from file by using PHAsset

前端 未结 2 662
半阙折子戏
半阙折子戏 2021-01-13 23:06

I have file at path

file:///var/mobile/Media/DCIM/100APPLE/IMG_0197.mov

But when I try this code-

NSError *error;
NSData *d         


        
2条回答
  •  梦谈多话
    2021-01-13 23:09

    For me helped the following code. It's important to use relativePath instead of absoluteString.

        [[PHImageManager defaultManager] requestAVAssetForVideo:videoContent options:options resultHandler:^(AVAsset* avasset, AVAudioMix* audioMix, NSDictionary* info){
                    AVURLAsset* myAsset = (AVURLAsset*)avasset;
                    NSData * data = [NSData dataWithContentsOfFile:myAsset.URL.relativePath];       
                    if (data) {
                    }
        }];
    

    Swift 3 version:-

    PHImageManager.default().requestAVAsset(forVideo: asset, options: nil, resultHandler: { (asset, mix, nil) in
                        let myAsset = asset as? AVURLAsset
                        do {
                            let videoData = try Data(contentsOf: (myAsset?.url)!)
                            self.selectedVideoData = videoData  //Set video data to nil in case of video
                            print("video data : \(videoData)")
    
                        } catch  {
                            print("exception catch at block - while uploading video")
                        }
                    })
    

提交回复
热议问题