我想阅读iOS中资产库文件的内容
使用资产NSFileHandle fileHandleForReadingFromUrl
网址的 defaultRepresentation
似乎始终返回0x0
...
我会一直在寻找解决方案。
编辑:
看起来Anomie的答案可能就是我想要的:
NSUInteger length = [representation getBytes:bytes fromOffset:0 length:[representation size] error:&error];
答案 0 :(得分:9)
对于较大的文件,您可能希望通过循环复制出来以读取块中的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;