Created
January 4, 2013 19:39
-
-
Save bclubb/4455289 to your computer and use it in GitHub Desktop.
Saving a file in chunks using a buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ALAssetRepresentation *video = [asset defaultRepresentation]; | |
| NSUInteger chunkSize = 100 * 1024; | |
| uint8_t *buffer = malloc(chunkSize * sizeof(uint8_t)); | |
| long length = [video size]; | |
| NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath: videoPath]; | |
| if(file == nil) { | |
| [[NSFileManager defaultManager] createFileAtPath:videoPath contents:nil attributes:nil]; | |
| file = [NSFileHandle fileHandleForWritingAtPath:videoPath]; | |
| } | |
| NSUInteger offset = 0; | |
| do { | |
| NSUInteger bytesCopied = [video getBytes:buffer fromOffset:offset length:chunkSize error:nil]; | |
| offset += bytesCopied; | |
| NSData *data = [[NSData alloc] initWithBytes:buffer length:bytesCopied]; | |
| [file writeData:data]; | |
| } while (offset < length); | |
| [file closeFile]; | |
| free(buffer); | |
| buffer = NULL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment