我正在使用最新的RNCryptor加密文件数据,然后将其保存到磁盘。
当我尝试加密大文件(大于150MB)时,我收到内存警告,并且内存增长非常快。
我尝试了以下解决方案,但都不适合我:
Memory issues when encrypting/decrypting a large file with RNCryptor on iOS
Dispatch queues and asynchronous RNCryptor
这是我的方法:
{
-(void)encryptFileDataWithFilePath:(NSString *)filePath
{
dispatch_semaphore_t信号量= 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)applyOperation:(CCOperation)operation
fromStream:(NSInputStream *)inStream
toStream:(NSOutputStream *)outStream
password:(NSString *)password
error:(NSError **)error {
将部分替换为注释:
// Generate the IV and salt, or read them from the stream
使用您的iv和密钥。
关于ios - 大文件的RNCryptor内存问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20219115/