title: iOS - Streaming large files for upload (application crashes when allocating too large files using NSData)
body: <p><strong>Introduction</strong></p>
  
  <p>I'm currently doing some bug fixes in an application which is in the style of Vimeo, that is, the user can record videos from the phone or iPad, and then upload the video for other users to see. The current problem have to do with uploading large files due to the way we are handling upload right now.</p>
  
  <p><strong>The Problem</strong></p>
  
  <p>So the problem is essentially that when uploading files to the server, we first allocate all the bytes that has to be uploaded in an <em>NSData</em> object. This string of bytes will then get attached to a standard HTTP Post message, and a receiving API will then handle it. <em>The problem is that with large file sizes (which videos will quickly be), the app will simply crash because the NSData object takes up too much memory on the iDevice.</em></p>
  
  <p>This is how the process works:</p>
  
  <pre><code>    Byte *buffer = malloc(content.defaultRepresentation.size);
  
      NSUInteger buffered = [content.defaultRepresentation getBytes:buffer fromOffset:0.0 length:content.defaultRepresentation.size error:nil];
  
      NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered];
  
      NSData *movieData = [NSData dataWithData:data];
  </code></pre>
  
  <p>I simply get the bytes from a video which has been saved in the iPhone or iPad's standard camera roll. Then I put these bytes into an NSData object. The string of bytes from this NSData object will then simply be attached to a simple HTTP post message and send to an API.</p>
  
  <p><strong>The Question</strong></p>
  
  <p>The question then is, and the problem I see, is that the entire byte string have to be send in a single HTTP Post message. So is there any way in which you can load in chunks of the movie file and append it to the post message, so you don't take up too much memory at a time? Or how could you go about doing this?</p>
  
  <p>Thank you for your time :)</p>