Obj-C 检查图像是否已存在于照片库中

Obj-C Check if image already exists in Photos gallery

在我的应用程序中,我必须实现保存图像功能。我是这样保存的:

  UIImage *image = [UIImage imageNamed:actualBackground];
  UIImageWriteToSavedPhotosAlbum(
      image, self,
      @selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:),
      nil);

/* ... */

- (void)thisImage:(UIImage *)image
    hasBeenSavedInPhotoAlbumWithError:(NSError *)error
                     usingContextInfo:(void *)ctxInfo {
  if (!error){
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    [self presentViewController:picker animated:YES completion:nil];
  }
}

不幸的是,我必须检查文件是否已经存在以防止重复保存。这也是必要的,因为多次保存同一图像不会覆盖一个文件,但它会创建它的副本...

你知道如何解决这个问题吗?

解决方案:

根据 Shravya Boggarapu 的回答,我将 assetUrl 存储在我的 NSUserDefaults 中。完整代码:

- (IBAction)onDownloadClick:(UIButton *)sender {
  UIImage *image = [UIImage imageNamed:actualBackground];

  NSString *key = [NSString stringWithFormat:@"assetsUrl %@", actualBackground];
  NSString *savedValue =
      [[NSUserDefaults standardUserDefaults] stringForKey:key];
  NSURL *url = [NSURL URLWithString:savedValue];
  if (url != nil) {
    PHFetchResult *fetch =
        [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:url]
                                    options:nil];
    if ([fetch count]) {
      UIAlertView *myAlert = [[UIAlertView alloc]
              initWithTitle:nil
                    message:NSLocalizedString(@"Already downloaded", nil)
                   delegate:self
          cancelButtonTitle:@"OK"
          otherButtonTitles:nil];
      [myAlert show];
      return;
    }
  }
  ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

  [library
      writeImageToSavedPhotosAlbum:image.CGImage
                       orientation:(ALAssetOrientation)image.imageOrientation
                   completionBlock:^(NSURL *assetURL, NSError *error) {

                     [library assetForURL:assetURL
                         resultBlock:^(ALAsset *asset) {
                           NSLog(@"assetURL %@", assetURL);
                           NSString *ass = [assetURL absoluteString];
                           [[NSUserDefaults standardUserDefaults]
                               setObject:ass
                                  forKey:key];
                           [[NSUserDefaults standardUserDefaults] synchronize];

                           UIAlertView *myAlert = [[UIAlertView alloc]
                                   initWithTitle:nil
                                         message:NSLocalizedString(
                                                     @"Image downloaded", nil)
                                        delegate:self
                               cancelButtonTitle:@"OK"
                               otherButtonTitles:nil];
                           [myAlert show];
                         }
                         failureBlock:^(NSError *error){
                         }];
                   }];
}

希望对大家有所帮助。

我有一个解决方案,但不是适用于所有情况的解决方案。

关于将图像保存到相机胶卷的事情是,当您将图像添加到相机胶卷时,会创建一个资产URL。此外,此资产 URL 每次都是新的,因此如果您保存到相机胶卷,它会创建一个副本。图片的名称也没有保留。

如果首先通过您的应用将图像添加到相机胶卷,则您可以将资产URL存储为图像信息的一部分。

在我的应用程序中,为每个包含一些关键信息的图像维护了一个字典。如果将图像保存到相机胶卷,这包括图像的资产URL。

获得 URL 后,您可以使用 fetchAssetsWithALAssetURLs:options: 函数检查它是否存在。

如果URL为nil或者fetched时的asset为nil,表示相册中不存在该图片。所以你可以重新添加。

没有简单的方法来比较两个图像,如果您发现一个将是一项繁重的任务,我们可以比较图像的元数据,我们可以将其写入图像并从图像中读取。

You can use this repo for that

而且 ALAssetLibrary 也有一些属性来访问图像元信息。如果你google有了这个,你一定会得到一些。

Like this one