iOS – Streaming large files for upload (application crashes when allocating too large files using NSData)-Collection of common programming errors
Introduction
I’m currently doing some bug fixes in an application which is in the style of Vimeo, that is, the user can record videos from the phone or iPad, and then upload the video for other users to see. The current problem have to do with uploading large files due to the way we are handling upload right now.
The Problem
So the problem is essentially that when uploading files to the server, we first allocate all the bytes that has to be uploaded in an NSData object. This string of bytes will then get attached to a standard HTTP Post message, and a receiving API will then handle it. The problem is that with large file sizes (which videos will quickly be), the app will simply crash because the NSData object takes up too much memory on the iDevice.
This is how the process works:
Byte *buffer = malloc(content.defaultRepresentation.size);
NSUInteger buffered = [content.defaultRepresentation getBytes:buffer fromOffset:0.0 length:content.defaultRepresentation.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered];
NSData *movieData = [NSData dataWithData:data];
I simply get the bytes from a video which has been saved in the iPhone or iPad’s standard camera roll. Then I put these bytes into an NSData object. The string of bytes from this NSData object will then simply be attached to a simple HTTP post message and send to an API.
The Question
The question then is, and the problem I see, is that the entire byte string have to be send in a single HTTP Post message. So is there any way in which you can load in chunks of the movie file and append it to the post message, so you don’t take up too much memory at a time? Or how could you go about doing this?
Thank you for your time 🙂