2

我需要检查接收到的文件是否存在于照片库中

下面我来解释一下

我通过 Wifi 网络将一张照片库图像/视频从一个 Ipad(发送者)传输/同步到另一个 Ipad(接收者)照片库。

我成功地完成了它。

但是我不需要接收设备的照片库中的重复文件。

因此,我创建了一个 MD5 字符串,该字符串对于文件(在发送方)始终是唯一的,并在传输文件之前将其发送到学习方。

在接收方,从 Sender 接收到 MD5 字符串(收到 MD5)后,我正在使用 ALAsset 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]
   ];  
}
4

1 回答 1

1

我建议您使用 SHA-1 进行散列。我相信它比 MD5 更快,而且您不必担心密码完整性,因为您没有保护任何东西,只是使用它来生成唯一密钥。

我的一个朋友写了一个有用的帮助类,出于同样的原因,他们在他们的应用程序(Sandvox)中使用它——查看两个文件是否相等。

看看 GitHub 上的KSCrypto

于 2011-09-07T06:37:22.727 回答