How to convert mp4 to NSData?

2019-08-30 18:11发布

问题:

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!



标签: ios video nsdata