收到的文件存在于照片库中
我需要检查照片库中是否存在收到的文件
让我在下面解释一下
我通过 Wifi 网络将一个照片库图像/视频从一台 Ipad(发送器)传输/同步到另一台 Ipad(接收器)照片库。
我成功地完成了它。
但是,我不需要接收设备的照片库中的重复文件。
因此,我创建了一个 MD5 字符串,该字符串对于文件(在发送方)始终是唯一的,并在传输文件之前将其发送到学习者方。
在接收方,从发送方收到 MD5 字符串(收到的 MD5)后,我使用 ALAsset Library 检索所有照片库文件,并为每个文件创建一个 MD5 字符串,这可能需要更多时间。将每个 MD5 字符串与收到的 MD5 进行比较细绳。
如果接收方照片库文件中的任何一个与接收到的MD5相等,则可以识别该文件存在于接收方照片库中。
处理速度与照片库中的文件数量有关。
如果照片库中的文件数量大于100,上述过程太慢。
所以我知道是否还有另一种方法/方式可以做到这一点。主要是我需要提高性能。请给我一个更好的概念。
my 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<[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]
];
}
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)(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 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]
];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用 SHA-1 进行哈希处理。我相信它比 MD5 更快,而且您不必担心加密完整性,因为您没有保护任何内容,只是使用它来生成唯一密钥。
我的一个朋友编写了一个有用的帮助器类,他们在他们的应用程序 (Sandvox) 中使用它来非常相同的原因 - 查看两个文件是否相等。
查看 GitHub 上的 KSCrypto。
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.