Received file exists in Photo Library
I need to check that the received file exists in Photo Library
Let me explain below
I transferred/synced one photo Library image/video from one Ipad(sender) to another Ipad(receiver) Photo Library through Wifi network.
I completed it successfully.
However I don't need duplicate files in the receiving device's Photo Library.
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.
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.
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.
The process speed is related to the number of files in photo Library.
The above process is too slow, if the number of files in photo Library is greater than 100.
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.
my Code
-(void)getAllURLofPhotLibraryImages:(NSString*)receivedImageFileMd5 {
if(urlStoreArr == nil){
urlStoreArr = [[NSMutableArray alloc] init];
}
void (^assetEnumerator)(str开发者_StackOverflowuct 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 string creation method
-(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]
];
}
I suggest you use SHA-1 for hashing. I believe it's faster than MD5 and you aren't worried about cryptographic integrity as you aren't securing anything, just using it to generate a unique key.
A friend of mine wrote a useful helper class, and they use it in their app (Sandvox) for the very same reason - to see if two files are equal.
Have a look at KSCrypto on GitHub.
精彩评论