尝试从图库中保存文档目录中的超高清图像。由于内存压力,我的应用程序崩溃了。如何将图像直接从alasset保存到文档文件夹而不考虑uiimage。
答案 0 :(得分:1)
如果您遇到内存问题,我建议您先阅读How can I release memory of UIImages no longer used。
如果您决定在没有使用UIImage的情况下仍需要复制,可以尝试使用
ALAsset *result; // do not forget to initialize it
ALAssetRepresentation *rawImage = [result defaultRepresentation];
uint8_t *buffer = malloc( rawImage.size );
[rawImage getBytes:buffer fromOffset:0 length:rawImage.size error:NULL];
NSData *d = [NSData dataWithBytes:buffer length:rawImage.size];
[d writeToFile:@"your_file_path_here" atomically:YES];
free(buffer);
更新:
以下代码可能更有效
long long sizeOfRawDataInBytes = rawImage.size;
NSMutableData* rawData = [[NSMutableData alloc]initWithCapacity:sizeOfRawDataInBytes];
void* bufferPointer = [rawData mutableBytes];
NSError* error=nil;
[rawImage getBytes:bufferPointer fromOffset:0 length:sizeOfRawDataInBytes error:&error];
if (error) {
NSLog(@"Getting bytes failed with error: %@",error);
}
else {
[rawData writeToFile: @"your_file_path_here" atomically:YES];
}