如何将 mp4 转换为 NSData? [英] How to convert mp4 to NSData?

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

问题描述

我在 XCode 中有名为 vvideo.mp4 的文件.我正在尝试将其转换为 NSData 如下

I have file in XCode with name vvideo.mp4. I am trying to convert it to NSData like below

    NSData *data = [NSData dataWithContentsOfFile:@"vvideo.mp4" options:nil error:nil];

但是当我记录数据时,它返回 null.

But as I log data, it returns null.

将任何视频转换为 NSData 的正确方法是什么?

What is right way to convert any video to NSData?

文件已在Xcode中添加并正确复制,您可以在这里看到.

File is added and copies properly in Xcode, you can see here.

编辑的问题....

推荐答案

dataWithContentsOfFile:options:error: 采用文件路径而不是文件名.如果视频文件在应用程序包中,您应该这样做,

dataWithContentsOfFile:options:error: takes a file path and not file name. If the video file is in application bundle you should do,

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"vvideo" ofType:@"mp4"];

NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile:filePath options:nil error:&error];
if(data == nil && error!=nil) {
    //Print error description
}

与其将 error 参数传递为 nil 传递值,不如将其用于了解错误情况(如果发生).

Rather than passing error argument as nil pass the value, it will be useful in understanding error condition, if any occurs.

希望有帮助!

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

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