IOS - Streaming large files to download (apps crash when distributing too large files using NSData)

Introduction

I am currently doing some bug fixes on an app that is Vimeo style, meaning a user can record a video from a phone or iPad and then upload the video for others to watch. The current issue is with uploading large files due to the way we are currently uploading the upload.

Problem

So the problem is that when uploading files to the server, we first allocate all the bytes that need to be loaded into the NSData object. This byte string will then be bound to standard HTTP post messages and then the API receiver will be processed. The problem is that with large file sizes (which will quickly appear in the video), the application will simply crash because the NSData object is taking 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 am just getting bytes from a video that was saved in a standard iPhone or iPad camera roll. Then I put those bytes into an NSData object. The byte string from this NSData object will simply bind to a simple HTTP post message and sent to the API.

Question

The question then becomes, and the problem I see is that the entire byte string needs to be sent in one HTTP Post. So, is there a way that you can download chunks of a movie file and append it to a message post so that you don't take up too much memory at the same time? Or how could you do it?

Thank you for your time:)

+3


source to share


1 answer


I would suggest that you use the ASIHTTPRequest library. It can download files directly from the phone rather than download the file to memory first. The logic here is to load the movie file by breaking it up into parts called multisets and then load them into the queue



+2


source







All Articles