【发布时间】:2014-03-27 06:53:21
【问题描述】:
尝试将超高清图像保存在图库中的文档目录中。我的应用程序由于内存压力而崩溃。如何将图像直接从alasset保存到documents文件夹而不进入uiimage。
【问题讨论】:
标签: ios iphone ipad uiimage save
尝试将超高清图像保存在图库中的文档目录中。我的应用程序由于内存压力而崩溃。如何将图像直接从alasset保存到documents文件夹而不进入uiimage。
【问题讨论】:
标签: ios iphone ipad uiimage save
如果您有记忆问题,我建议您先阅读此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];
}
【讨论】: