我的程序的一部分读取目录,然后计算文件夹中每个文件的哈希值。每个文件都加载到内存中,我不知道如何释放它。我在这里阅读了很多主题,但找不到正确的答案。有人可以帮忙吗?
#import "MD5.h"
...
NSFileManager * fileMan = [[NSFileManager alloc] init];
NSArray * files = [fileMan subpathsOfDirectoryAtPath:fullPath error:nil];
if (files)
{
for(int index=0;index<files.count;index++)
{
NSString * file = [files objectAtIndex:index];
NSString * fullFileName = [fullPath stringByAppendingString:file];
if( [[file pathExtension] compare: @"JPG"] == NSOrderedSame )
{
NSData * nsData = [NSData dataWithContentsOfFile:fullFileName];
if (nsData)
{
[names addObject:[NSString stringWithString:[nsData MD5]]];
NSLog(@"%@", [nsData MD5]);
}
}
}
}
和MD5.m
#import <CommonCrypto/CommonDigest.h>
@implementation NSData(MD5)
- (NSString*)MD5
{
// Create byte array of unsigned chars
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
// Create 16 byte MD5 hash value, store in buffer
CC_MD5(self.bytes, (uint)self.length, md5Buffer);
// Convert unsigned char buffer to NSString of hex values
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x",md5Buffer[i]];
return output;
}
@end