我正在使用最新的 RNCryptor 加密文件数据,然后将其保存到磁盘。 当我尝试加密大文件(超过 150MB)时,我收到内存警告并且内存增长非常快。
我尝试了以下解决方案,但没有一个对我有用: Memory issues when encrypting/decrypting a large file with RNCryptor on iOS Dispatch queues and asynchronous RNCryptor
这是我的方法: { - (void)encryptFileDataWithFilePathNSString *)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];
}
有什么想法吗? 谢谢!
您应该能够轻松地使用 RNCryptManager
方法作为示例:
+ (BOOL)applyOperationCCOperation)operation
fromStreamNSInputStream *)inStream
toStreamNSOutputStream *)outStream
passwordNSString *)password
errorNSError **)error {
用评论替换部分:
// Generate the IV and salt, or read them from the stream
使用您的 iv 和 key 。
关于ios - 大文件的 RNCryptor 内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20219115/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |