ios - 大文件的 RNCryptor 内存问题
<p><p>我正在使用最新的 RNCryptor 加密文件数据,然后将其保存到磁盘。
当我尝试加密大文件(超过 150MB)时,我收到内存警告并且内存增长非常快。</p>
<p>我尝试了以下解决方案,但没有一个对我有用:
<a href="https://stackoverflow.com/questions/15335803/memory-issues-when-encrypting-decrypting-a-large-file-with-rncryptor-on-ios" rel="noreferrer noopener nofollow">Memory issues when encrypting/decrypting a large file with RNCryptor on iOS</a>
<a href="https://stackoverflow.com/questions/14481605/dispatch-queues-and-asynchronous-rncryptor/14571838#14571838" rel="noreferrer noopener nofollow">Dispatch queues and asynchronous RNCryptor</a> </p>
<p>这是我的方法:
{
- (void)encryptFileDataWithFilePath:(NSString *)filePath
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);</p>
<pre><code>__block int total = 0;
int blockSize = 32 * 1024;
__block NSInputStream *inputStream = ;
__block NSOutputStream *outputStream = ] append:NO];
__block NSError *encryptionError = nil;
;
;
RNEncryptor *encryptor = [ initWithSettings:kRNCryptorAES256Settings
password:self.loginManager.passcode
handler:^(RNCryptor *cryptor, NSData *data)
{
@autoreleasepool
{
;
dispatch_semaphore_signal(semaphore);
data = nil;
if (cryptor.isFinished)
{
;
encryptionError = cryptor.error;
// call my delegate that I'm finished with decrypting
}
}
}];
NSData *data = nil;
while (inputStream.hasBytesAvailable)
{
@autoreleasepool
{
uint8_t buf;
NSUInteger bytesRead = ;
if (bytesRead > 0)
{
data = ;
total = total + bytesRead;
;
LogError(@"%.2f",(float)data.length/1024.0f/1024.0f);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
}
}
;
;
</code></pre>
<p>}</p>
<p>有什么想法吗?
谢谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您应该能够轻松地使用 <code>RNCryptManager</code> 方法作为示例:</p>
<pre><code>+ (BOOL)applyOperation:(CCOperation)operation
fromStream:(NSInputStream *)inStream
toStream:(NSOutputStream *)outStream
password:(NSString *)password
error:(NSError **)error {
</code></pre>
<p>用评论替换部分:</p>
<pre><code> // Generate the IV and salt, or read them from the stream
</code></pre>
<p>使用您的 iv 和 key 。</p></p>
<p style="font-size: 20px;">关于ios - 大文件的 RNCryptor 内存问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20219115/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20219115/
</a>
</p>
页:
[1]