title: iOS how to upload a large asset file into sever by streaming
body: <p>I am new iOS programmer.<br>
  I want to upload a large file(video or image) from <strong>asset library</strong> into sever,
  My original way is just use <strong>NSMutableURLRequest</strong> and append <strong>NSData</strong>(large video or large image) to it, and crash happened in the following code:</p>
  
  <pre><code>    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset){
          //.......some code I just skip it...
          ALAssetRepresentation *rep = [asset defaultRepresentation];
          void *buffer = [rawData mutableBytes];
          [rep getBytes:buffer fromOffset:0 length:size error:nil];
          NSData *videoData = [[NSData alloc] initWithBytes:buffer length:size];//crash here
          [self startUploading:videoData];
      }
  </code></pre>
  
  <p>I know this crash is because memory is not enough, video file can't just alloc to NSData.<br> 
  I've google this for 2 days, and sounds there are several ways to solve this.</p>
  
  <ol>
  <li>use third party library: such like AFNetworking, ASIHTTPRequest(but I don't want to use it,because don't know when will this stop maintaining or updating )</li>
  <li>use streaming to upload large file</li>
  </ol>
  
  <p>and I want to use streaming way(point 2) to do upload stuff,<br>
  I found this link:  <a href="http://zh.scribd.com/doc/51504708/10/Upload-Files-Over-HTTP" rel="nofollow noreferrer">http://zh.scribd.com/doc/51504708/10/Upload-Files-Over-HTTP</a><br>
  Look like can solve my problem, but still not very clear to know how to do<br><br></p>
  
  <p><strong>Question1</strong>:
  there is an example in that link, the upload file is from bundle<br>
  how to make asset into stream? or copy asset to APP's folder?<br>
  I found this link <a href="https://stackoverflow.com/questions/10967549/copy-image-from-assets-library-to-an-app-folder">Copy image from assets-library to an app folder</a><br> but still can not find the way.<br></p>
  
  <p><strong>Question2</strong>:
  or Is there any other more clear streaming example for uploading large file?<br><br></p>
  
  <p><br><br>
  Thank you for your passion 
  <br><br>
  <strong>updated1</strong>: after I implemented needNewBodyStream delegate,  "request stream exhaustion" message seemed to solved, but instead, meet another "Error Domain=kCFErrorDomainCFNetwork Code=303 "The operation couldn't be completed." how to solve it? </p>
  
  <pre><code>-(NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request
  {
      [NSThread sleepForTimeInterval:2];
      NSInputStream *fileStream = [NSInputStream inputStreamWithFileAtPath:pathToBodyFile];
      if (fileStream == nil) {
          NSLog(@"NSURLConnection was asked to retransmit a new body stream for a request. returning nil!");
      }
      return fileStream;
  }
  </code></pre>