title: Received file exists in Photo Library
body: <p>I need to check that the received file exists in Photo Library</p>
  
  <p>Let me explain below</p>
  
  <p>I transferred/synced one photo Library image/video from one Ipad(sender) to another Ipad(receiver) Photo Library through Wifi network.</p>
  
  <p>I completed it successfully.</p>
  
  <p>However I don't need duplicate files in the receiving device's Photo Library.</p>
  
  <p>So I created a MD5 string which is always unique for the file(in sender side) and sent it to learner side before transferring the file.</p>
  
  <p>On the receiver side, after receiving the MD5 string(received MD5) from Sender, I am retrieving all  the Photo Library files using ALAsset Library and creating a MD5 string for each files which may take more time.Comparing each MD5 string with the received MD5 string.</p>
  
  <p>If any of the receiver side photo library file MD5 is equal to the received MD5, we can identify that the file exists in the receiver photo library.</p>
  
  <p>The process speed is related to the number of files in photo Library.</p>
  
  <p>The above process is too slow, if the number of files in photo Library is greater than 100.</p>
  
  <p>So that I know is there another method/way to do it.mainly I need to improve the performance. Please provide me with a better concept. </p>
  
  <p><strong>my Code</strong></p>
  
  <hr>
  
  <pre><code>-(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&lt;[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;
    }
  
  
  
  }
  </code></pre>
  
  <p>//Md5 string creation method</p>
  
  <pre><code> -(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]
     ];  
  }
  </code></pre>