如何将视频路径转换为 ​​NSData [英] How to convert video path to NSData

查看:124
本文介绍了如何将视频路径转换为 ​​NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 DKImagePicker 从图库中获取多个视频.能够获取视频但不能转换为 NSData.并且还从服务器收到错误.

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"}}

可能没有正确转换为 NSData 或者可能是其他一些问题.到目前为止我尝试的是:

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
            }

请指导.提前致谢.

推荐答案

当您从委托方法获取文件路径时,您应该立即将文件复制到 Documents 或任何其他位置.因为最初文件被放置在私人文件夹中,据我所知这是临时的.因此,当您的第 3 方工具完成处理并存储到文件中时 - 它在某些时候变得无法访问.所以在你使用 copyItemAtPath:toPath: 或类似的东西复制它之后 - 你可以通过从新路径读取它来获得 NSData.

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.

从 DKImagePicker git 我找到了方法:writeAVToFile(path:presetName:completeBlock:) 这可能会有所帮助.查看此处.

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

这篇关于如何将视频路径转换为 ​​NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆