How to convert video path to NSData

2019-09-16 17:30发布

I have used DKImagePicker for fetching multiple videos from gallery. Able to fetch video but not getting convert to NSData. And also receiving error from server.

Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0040.MOV” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0040.MOV, NSUnderlyingError=0x172e5f6b0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

May be not converted to NSData properly or may be some other issue. what i tried so far is:

let videoURL = User.sharedInstance.arrRoomGalleryVideos.objectAtIndex(index) as? NSURL
            var movieData: NSData?
            do {
                let video = try NSData(contentsOfURL: videoURL!, options: .DataReadingMappedIfSafe)
                print("video", video)
                multipartFormData.appendBodyPart(data: video, name: "video_path[]", fileName: strVidName, mimeType: "mp4")
            } catch {
                print(error)
                return
            }

Please guide. Thanks in advance.

1条回答
smile是对你的礼貌
2楼-- · 2019-09-16 18:18

You should copy file into Documents or any other place IMMEDIATELY when you get file path from delegate method. Because originally file was placed into private folder, which is temporary as I understand. So when your 3-rd party tool finishes processing and stores into the file - it becomes inaccessible at some point. So after you copy it using copyItemAtPath:toPath: or something like that - you may get NSData by reading it from new path.

From DKImagePicker git I found method: writeAVToFile(path:presetName:completeBlock:) which may help. Look here.

查看更多
登录 后发表回答