How to convert video path to NSData

放肆的年华 提交于 2019-12-25 09:16:52

问题


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:


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.



来源:https://stackoverflow.com/questions/39591423/how-to-convert-video-path-to-nsdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!