对于较大的文件,您可能希望通过循环复制以读取块中的 X 字节,否则您可能会耗尽设备内存。
NSUInteger chunkSize = 100 * 1024;
uint8_t *buffer = malloc(chunkSize * sizeof(uint8_t));
ALAssetRepresentation *rep = [myasset defaultRepresentation];
NSUInteger length = [rep size];
NSFileHandle *file = [[NSFileHandle fileHandleForWritingAtPath: tempFile] retain];
if(file == nil) {
[[NSFileManager defaultManager] createFileAtPath:tempFile contents:nil attributes:nil];
file = [[NSFileHandle fileHandleForWritingAtPath:tempFile] retain];
}
NSUInteger offset = 0;
do {
NSUInteger bytesCopied = [rep getBytes:buffer fromOffset:offset length:chunkSize error:nil];
offset += bytesCopied;
NSData *data = [[NSData alloc] initWithBytes:buffer length:bytesCopied];
[file writeData:data];
[data release];
} while (offset < length);
[file closeFile];
[file release];
free(buffer);
buffer = NULL;