Reputation: 6152
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.
Upvotes: 5
Views: 3512
Reputation: 8029
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
Upvotes: 2
Reputation: 13192
You simple can't and do not want to hold hundreds of MBs in the memory. This is not the way of uploading a video. First of all I suggest you to configure the video recording parameters so that the resulting file would be smaller even if the quality of the video will be lower. Your users won't want to upload 2-500 MBs. But even so the resulting file will be too big to upload it "from the memory". You should instead read a fraction of it, say 100kByte, upload it to the server, read the next part, upload it, etc.
Lucky you there are ready solutions for this. If you have to support only iOS 7, check out NSURLSessionUploadTask
. If you have to support also older versions, probably the simplest way is to use AFNetworking. Check out "Multi-Part Request" on this page: http://cocoadocs.org/docsets/AFNetworking/2.1.0/
Upvotes: 0