iOS ALAssetsLibrary и NSFileHandle для чтения содержимого файла
Я хочу прочитать содержимое файла библиотеки активов в iOS
NSFileHandle fileHandleForReadingFromUrl
используя актив defaultRepresentation
URL, кажется, всегда возвращается 0x0
...
Я буду продолжать искать решение в то же время.
РЕДАКТИРОВАТЬ:
Похоже, что ответ от Аноми может быть тем, что я хочу:
NSUInteger length = [representation getBytes:bytes fromOffset:0 length:[representation size] error:&error];
1 ответ
Решение
Для больших файлов вы, вероятно, захотите скопировать их через цикл, чтобы прочитать 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;