我正在使用最新的 RNCryptor 加密文件数据,然后将其保存到磁盘。当我尝试加密大文件(超过 150MB)时,我收到内存警告并且内存增长非常快。
我尝试了以下解决方案,但它们都不适合我: 在 iOS Dispatch queues 和异步 RNCryptor上使用 RNCryptor 加密/解密大文件时出现内存问题
这是我的方法: { - (void)encryptFileDataWithFilePath:(NSString *)filePath { dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block int total = 0;
int blockSize = 32 * 1024;
__block NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:filePath];
__block NSOutputStream *outputStream = [NSOutputStream outputStreamWithURL:[NSURL URLWithString:[filePath stringByAppendingString:@"1"]] append:NO];
__block NSError *encryptionError = nil;
[inputStream open];
[outputStream open];
RNEncryptor *encryptor = [[RNEncryptor alloc] initWithSettings:kRNCryptorAES256Settings
password:self.loginManager.passcode
handler:^(RNCryptor *cryptor, NSData *data)
{
@autoreleasepool
{
[outputStream write:data.bytes maxLength:data.length];
dispatch_semaphore_signal(semaphore);
data = nil;
if (cryptor.isFinished)
{
[outputStream close];
encryptionError = cryptor.error;
// call my delegate that I'm finished with decrypting
}
}
}];
NSData *data = nil;
while (inputStream.hasBytesAvailable)
{
@autoreleasepool
{
uint8_t buf[blockSize];
NSUInteger bytesRead = [inputStream read:buf maxLength:blockSize];
if (bytesRead > 0)
{
data = [NSData dataWithBytes:buf length:bytesRead];
total = total + bytesRead;
[encryptor addData:data];
LogError(@"%.2f",(float)data.length/1024.0f/1024.0f);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
}
}
[inputStream close];
[encryptor finish];
}
任何想法 ?谢谢!