我需要检查照片库中是否存在收到的文件
让我解释一下
我通过Wifi网络将一张照片库图像/视频从一张Ipad(发件人)转移/同步到另一张Ipad(接收器)照片库。
我成功完成了它。
但是我不需要在接收设备的照片库中重复文件。
所以我创建了一个MD5字符串,该字符串对于文件(发送方)始终是唯一的,并在传输文件之前将其发送给学习者。
在接收方,从Sender收到MD5字符串(收到MD5)后,我正在使用ALAsset Library检索所有Photo Library文件,并为每个文件创建一个可能需要更多时间的MD5字符串。比较每个MD5字符串收到的MD5字符串。
如果任何接收方侧照片库文件MD5等于收到的MD5,我们可以识别该文件存在于接收方照片库中。
处理速度与照片库中的文件数有关。
如果照片库中的文件数量大于100,则上述过程太慢。
所以我知道还有另一种方法/方法可以做到。主要是我需要提高性能。请给我一个更好的概念。
我的代码
-(void)getAllURLofPhotLibraryImages:(NSString*)receivedImageFileMd5 {
if(urlStoreArr == nil){
urlStoreArr = [[NSMutableArray alloc] init];
}
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(@"See Asset: %@", result);
// assets is a NSMutableArray..
// Here storing the asset's image URL's in NSMutablearray urlStoreArr
NSURL *url = [[result defaultRepresentation] url];
[urlStoreArr addObject:url];
NSLog(@"Adding all the Photolibrary URL's");
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
else {
NSLog(@"going to check FileExistInPhotoLibrary");
[self CheckFileExistInPhotoLibrary:receivedImageFileMd5];
//call the method from here.
}
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(@"Failure");
}];
}
-(void)CheckFileExistInPhotoLibrary:(NSString *)receivedImageFileMd5{
if([urlStoreArr count] == 0){
NSLog(@"file is not exist in PhotoLibrary");
return;
}
int j = 1;
isFileFoundInPhotoLib = NO;
for(int counts =0;counts<[urlStoreArr count];counts++)
{
NSLog(@"entered in to for loop");
NSLog(@"%d",[urlStoreArr count]);
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
UIImage *photLibraryImage = [UIImage imageWithCGImage:iref];
if(photLibraryImage){
NSData *imageData = UIImagePNGRepresentation(photLibraryImage);
NSString *photLibImageMd5 = [self md5Image:imageData];//creating MD5 string for receiver side phpto library images.
NSLog(@"photolib MD5::%@",photLibImageMd5);
NSLog(@"ReceivedImageMD5:%@",receivedImageFileMd5);
if([photLibImageMd5 isEqualToString:receivedImageFileMd5])
{
NSLog(@"file is exist in PhotoLibrary");
return;
}
if(j == [urlStoreArr count]){
NSLog(@"file is not exist in PhotoLibrary");
}
}
}
};
//
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
[self RequestForTheFile:fileTransferResponse];
NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:[urlStoreArr objectAtIndex:counts]
resultBlock:resultblock
failureBlock:failureblock];
j++;
}
if(urlStoreArr != nil){
[urlStoreArr release];
NSLog(@"urlstore array released");
urlStoreArr = nil;
}
}
// Md5字符串创建方法
-(NSString *) md5Image:(NSData *)data {
return [self md5:data];
}
- (NSString*)md5:(NSData *)data
{
unsigned char result[16];
CC_MD5( data.bytes, data.length, result ); // This is the md5 call
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}