How to convert mp4 to NSData?

半城伤御伤魂 提交于 2019-12-25 09:35:07

问题


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];

But as I log data, it returns null.

What is right way to convert any video to NSData?

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

EDITED QUESTION....


回答1:


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
}

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

Hope that helps!



来源:https://stackoverflow.com/questions/19565373/how-to-convert-mp4-to-nsdata

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