上传或导出大型视频会导致内存崩溃并导致崩溃...我该如何分解?

aka*_*aru 6 iphone avfoundation nsdata ios4

我已经实现了一种使用多部分帖子将视频上传到youtube等的方法,或者将视频保存到本地的相机胶卷.但是,对于大型视频,由于内存占用太大,我会受到监视,因为目前我必须将整个视频放入内存才能发布或保存.

我可以采取哪些步骤将大型视频分解为可管理的块?

Dan*_*iel 1

您可以将视频保存到文件中,并使用 nsstream 读取视频块并发送它们,您必须保持某种状态来记住您发送的内容和剩下的内容,但实施起来应该不会太糟糕,例如

  BOOL done=FALSE;
   NSInputStream *stream=nil;

   NSString *myFile=@"..."; //your file path
   stream=[[NSInputStream alloc] initWithFileAtPath:myFile ];
  while(!done)
  {


   int chunkSize=500000; //500 kb chunks 
   uint8_t buf[chunkSize];
      //reads into the buffer and returns size read
   NSInteger size=[stream read:buf maxLength:chunkSize];

   NSLog(@"read %d bytes)", size);

    NSData * datum=[[NSData alloc] initWithBytes:buf length:size];
      //when we actually read something thats less than our chunk size we are done
   if(size<chunkSize)
     done=TRUE;
   //send data      
   }
Run Code Online (Sandbox Code Playgroud)