testa abalez
testa abalez

Reputation: 1062

How to handle large files with NSData?

I have a very large video and I need to chunk this video to upload it to Dropbox.

I tried to use NSData, but because this file is too large, my application always crashes, so I don't know what I can do now.

For smaller videos, I used this:

NSData(contentsOfURL: self.newAsset.URL)!.subdataWithRange(NSMakeRange(0, 10000000))

and I didn't have any problem with that, but when the video is too large I have an error:

Cannot allocate memory

So, what can I do to chunk the data of large videos?

Upvotes: 5

Views: 1091

Answers (2)

Buntylm
Buntylm

Reputation: 7383

For best practice go with NSURLSession if you want to implement custom otherwise lots for third party library are there like RESTKit or AFNetworking. For NSURLSession the session NSURLSession supports three types of tasks: data tasks, download tasks, and upload tasks. All it support the background uploads/downloads as well. source(apple developer)

  • Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests from your app to a server. Data tasks can return data to your app one piece at a time after each piece of data is received, or all at once through a completion handler.
  • Download tasks retrieve data in the form of a file, and support background downloads while the app is not running.
  • Upload tasks send data in the form of a file, and support background uploads while the app is not running.

Image Source raywenderlich.com Image Source:

Upvotes: 2

Pandey_Laxman
Pandey_Laxman

Reputation: 3908

You should use video file url to upload large data using NSURLSession

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL;

Upvotes: 0

Related Questions