IOS 7 base64 video encoding

2k views Asked by At

I'm having an issue when I try to encode a video in base64 within iOS 7.

I am not sure if my NSdata is being set correctly.

The video is playing on my iOS simulator, it's fine though. However when the code reaches the NSData block it is doing nothing and my logs for data and base64String are returning null.

Please check my code out

//Try to encode Video
//Path of the video
NSString *url = [[NSBundle mainBundle] pathForResource:@"trailer_iphone"  ofType:@"m4v"];
//startthe player
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(movieFinishCallBack) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
//set the frame
player.view.frame = CGRectMake(10,10,300,300);
[self.view addSubview:player.view];
//start to play
[player play];

NSData *data = [[NSData dataWithContentsOfFile:@"trailer_iphone.m4v" ]base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *base64String = [data base64EncodedStringWithOptions:2];
NSLog(@"data = %@ base64String = %@",data, base64String);

Many Thanks!

1

There are 1 answers

0
Adjaxon Araújo On BEST ANSWER

I figure it out!!!!! In the NSData I cannot assign the a content as a File straightaway. First the video path must be declared, like a video player. For Example:

NSString *path = [[NSBundle mainBundle] pathForResource:@"trailer_iphone" ofType:@"m4v"];

Than we declare the NSData referencing the previous path like this:

NSData *data = [[NSData dataWithContentsOfFile:path]

To finalize we just need to set the iOS7 base64 encode support:

NSData *data = [[NSData dataWithContentsOfFile:path]base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];

That's it!!! Totally encoded!!!

If you wish to break your code in small chunks just see this link!

My final method is this one:

-(void)videoBreak{     
 NSString *url = [[NSBundle mainBundle] pathForResource:@"trailer_iphone" ofType:@"m4v"];    
 NSData *data = [[NSData dataWithContentsOfFile:url]base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];    
    NSUInteger length = [data length];
    NSLog(@"First log %@ length = %i",data,length);
    NSUInteger chunkSize = 4 * 1024;
    NSUInteger offset = 0;
    do {
    NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
    NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[data bytes] + offset
    length:thisChunkSize
    freeWhenDone:NO];
    offset += thisChunkSize;
    // do something with chunk        
    NSLog(@"chunk = %@ \"n\"offset = %i \"n\"thisChunkSize = %i \"n\"lenght = %i",chunk,     offset,thisChunkSize, length);
    } while (offset < length);
    }