How to get slice data of a huge video file

I want to upload a video file to NSData if I use dataWithContentsOfFile: it will waste a lot of memory when the file is too large. How can I get specific file data with offset and length. I need to upload a video file to parts.

+3
source share
3 answers

You can use NSFileHandle , and then look for a seekToFileOffset: and read a piece of data using readDataOfLength: .

+1
source

it

NSData *data = [NSData dataWithContentsOfMappedFile:<path>];
NSRange first4k = {0, MIN([data length], 4096)};
return [data subdataWithRange:first4k];

gives you only the first 4k file without downloading it.

But dataWithContentsOfMappedFile: is deprecated in iOS5.

I suppose - WARNING: untested! -

+ (id)dataWithContentsOfURL:(NSURL *)aURL options:(NSDataReadingOptions)mask error:(NSError **)errorPtr

with a mask mounted on

NSDataReadingMappedIfSafe

NSDataReadingMappedAlways

.

iOS5.

+1

Here's an article that can help Cocoa - my girlfriend

A few weeks ago, Matt Long had a problem with a lack of memory in the application. He had a ginormous data file that he needed to load and process, and this memory loss was more than the application could bring. It will load just fine in NSData, but before it can finish with it, the application will run out of memory and die.

0
source

All Articles