【问题标题】:Obj-C Check if image already exists in Photos galleryObj-C 检查照片库中是否已存在图像
【发布时间】:2015-09-23 17:06:49
【问题描述】:

在我的应用程序中,我必须实现保存图像功能。我已经管理过这样的保存:

  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){
                         }];
                   }];
}

希望它会帮助别人。

【问题讨论】:

  • 如何在 Swift 中做到这一点?

标签: ios objective-c image gallery


【解决方案1】:

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

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

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

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

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

如果 URL 为 nil 或获取时的资产为 nil,则表示该图像不存在于相机胶卷中。所以你可以重新添加。

【讨论】:

    【解决方案2】:

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

    You can use this repo for that

    并且 ALAssetLibrary 也有一些属性来访问图像元信息。如果你用这个谷歌,你会得到一些。

    Like this one

    【讨论】:

    • 资产库函数已从 iOS 9 中弃用
    • 是的,我不知道,但现在你可以使用 Photos.framework,它将提供与 PHAsset 等相同的功能。
    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 2019-02-20
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    相关资源
    最近更新 更多