Skip to content

Instantly share code, notes, and snippets.

@bclubb
Created January 4, 2013 19:39
Show Gist options
  • Select an option

  • Save bclubb/4455289 to your computer and use it in GitHub Desktop.

Select an option

Save bclubb/4455289 to your computer and use it in GitHub Desktop.
Saving a file in chunks using a buffer
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