Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload large video in iOS?

I am working on a project where I have to upload a large video to a server. I tried to hold the video in a variable. I am currently doing it using below code.

NSData *data=[NSData dataWithContentsOfFile:self.path];

It works on small video. But my app crashes when the video exceed the size 200-500 MB. I have hold video in a variable to provide upload functionality to different social networking sites and dropbox.

The message I get is Terminated due to Memory Error.

Please advice. How can I achieve this.

Edited

I have to hold video in NSData because I need to upload the video in different social networking sites such as Facebook, Dropbox, Google Drive etc through there API's. There APIs uses NSData for holding the binary data of the video. So I believe I can not use AFNetworking, NSInput Stream or any other mechanism here.

like image 875
Sanchit Paurush Avatar asked Nov 02 '22 05:11

Sanchit Paurush


1 Answers

Take a look at NSInputStream and set the HTTPBodyStream of an NSMutableURLRequest to the input stream of the video - this will grab what data it needs from the disk and upload it.

e.g. If you have use AFNetworking you could do something similar to the following:

NSMutableURLRequest *request = [afNetworkingHTTPClient requestWithMethod:@"POST" path:@"endpoint/path" parameters:nil];
request.HTTPBodyStream = [[NSInputStream alloc] initWithFileAtPath:@"path/to/your/file"];

//... Configure your request here

AFHTTPRequestOperation *HTTPOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];


//... Set the success / failure blocks on the operation to check its state

[afNetworkingHTTPClient enqueueHTTPRequestOperation:HTTPOperation];

Links

  • NSInputStream
  • NSMutableURLRequest
like image 79
Oliver Atkinson Avatar answered Nov 08 '22 11:11

Oliver Atkinson