2011-09-07 41 views
2

我需要檢查接收到的文件中的圖片庫中存在接收的文件在照片庫中存在

讓我來解釋

我轉/同步一個圖片庫的圖像/視頻從一ipad公司(發件人)至另一個Ipad(接收器)照片庫通過Wifi網絡。

我成功地完成了它。

但是我不需要接收設備的照片庫中的重複文件。

所以我創建了一個MD5字符串,它始終是文件(在發送端)獨特的,把它傳輸文件前學習者的一面。

在接收端,收到來自發件人的MD5字符串(收到MD5)後,我使用ALAsset庫檢索所有照片庫文件,併爲每個文件創建一個MD5字符串,這可能需要更多時間。比較每個MD5字符串與收到的MD5字符串。

如果任何接收方的照片庫文件MD5是等於接收到的MD5,我們可以識別文件中的接收器照片庫中存在。

處理速度是關係到照片庫的文件的數量。

上述過程太慢,如果在照片庫中的文件數量大於100

所以,我知道有另一種方法/辦法做到it.mainly我需要提高性能。請給我一個更好的概念。

我的代碼


-(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] 
    ]; 
} 
+0

的可能重複的[保存在不同的應用程序相同的名稱相同的圖像,而無需給出選項基於i墊手動((**輸入圖像名稱**)](http://stackoverflow.com/questions/6492803 /保存最相同的圖像與 - 相同名稱的功能於不同的應用程式,不給惠-A選項對) –

回答

1

我建議你使用SHA-1哈希計算。我相信它比MD5更快,你不擔心密碼的完整性,因爲你不能保證任何東西,只是用它來生成一個唯一的密鑰。

我的一位朋友寫了一個有用的幫手類,他們在他們的應用程序(Sandvox)中使用它,原因很相同 - 查看兩個文件是否相等。

看一看KSCrypto GitHub上。