Uploading Large NSData to the Web

后端 未结 3 1940
遇见更好的自我
遇见更好的自我 2020-12-29 16:33

I\'m currently working on an application that has to upload large files (mainly movies/videos) to the web. After reading what I can, I went the the approach of converting t

3条回答
  •  忘掉有多难
    2020-12-29 16:54

    Assuming that it makes sense to upload the movie in its native format, you can really make this easier using the BSD (ie Unix) section 3 interface:

    • given a filePath, open the file and get an int file descriptor (fd)

    • with fd, get the length of the file

    • keep track of how much you've loaded so you know where to get more data

    • use mmap(3) to map in JUST the data you want to upload at any time, and use the void * pointer returned by mmap as the location of the data

    • when the data has been sent, munmap the old data chunk and mmap a new chunk

    • after all data is sent, munmap the last chunk, the close(fd).

    No temporary memory - no mallocs. I use mmap whenever I have to deal with huge files.

    Edit: you can also use NSData dataWithContentsOfFile:options with options set to use mmap. You would then use the byte pointer to read small chunks as you need them.

提交回复
热议问题