Writing huge chunks of data to NSData-iOS objects

I have a video file about 2 GB in size. This video header is encrypted (approximately 528 bytes encrypted). To decrypt this video file, I read all the bytes from the file into an NSData object. As soon as I write this file to an NSData object, my application will work (maybe b'coz max-256MB RAM for iPad).

So, how can I temporarily save this NSData object to the iPad / iPhone virtual memory?

Any other approach with which I can achieve the same?

+7
source share
1 answer

Use NSInputStream to read in a file in parts so that you don't load all of this into memory at once. In particular, you will want to use hasBytesAvailable and read:maxLength:

Something like:

 NSInputStream *myStream = [NSInputStream inputStreamWithFilAtPath:pathToAbsurdlyLargeFile]; [myStream open]; Byte buffer[BUFFER_SIZE]; while ([myStream hasBytesAvailable]) { int bytesRead = [myStream read:buffer maxLength:BUFFER_SIZE]; NSData *myData = [NSData dataWithBytes:buffer length:bytesRead]; // do other stuff... } [myStream close]; 

Note that you may not need to create an NSData object. You just mentioned that you use it, so I dropped it.

+11
source

All Articles