IOS 7 base64视频编码

时间:2013-12-19 00:41:17

标签: video ios7 base64 mpmovieplayercontroller video-encoding

当我尝试在iOS 7中的base64中对视频进行编码时,我遇到了问题。

我不确定我的NSdata是否设置正确。

视频正在我的iOS模拟器上播放,但它很好。但是,当代码到达NSData块时,它什么都不做,我的数据和base64String的日志返回null。

请检查我的代码

//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);

非常感谢!

1 个答案:

答案 0 :(得分:4)

我明白了!!!!! 在NSData中,我不能直接将内容分配为文件。 首先必须声明视频路径,就像视频播放器一样。 例如:

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

我们声明引用前一个路径的NSData如下:

  
NSData *data = [[NSData dataWithContentsOfFile:path]
  

要完成,我们只需要设置iOS7 base64编码支持:

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

就是这样!完全编码!!!

如果您希望以小块的形式破解代码,请参阅此link

我的最后一个方法就是这个:

  
-(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);
    }