Search code examples
iosmemory-managementdownloadnsurlconnectionmpmovieplayercontroller

iPhone Downloading large size videos to Documents Directory(~300 mb)


Need your suggestions/advice on a scenario I am stuck into. I am developing an iPhone Application which has 12-15 videos. User can download any video and then play it on his iPhone. The problem is that I am writing the videos to the documents directory. It works well for small videos(~50 mb) but when the bigger ones are choosen(~250 mb) the app crashes, due to low memory warnings.


I know that the app is crashing due to increase in memory footprint. Is there any alternate way to download large files more efficiently(in chunks) with less memory utilization? Otherwise I am thinking to give download option only for small videos and only streaming option for large size videos.


Solution

  • 1) Use an asynchronous NSUrlConnection object to download the video.

    2) create a file to save the video beforehand, get the file descriptor, and set the F_NOCACHE flag on the open file so as to not consume memory in the disk cache.

    3) as you get small chunks of data as NSData objects in the delegate call, append them to the open file.

    4) when the download completes, close the file.

    You may want to verify that the device has enough space to accept the download; there are posts here on how to do that.