代码之家  ›  专栏  ›  技术社区  ›  Lau

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

  •  4
  • Lau  · 技术社区  · 10 年前

    在我的应用程序中,我必须实现保存图像功能。我已经做到了这样的储蓄:

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

    希望它能帮助一些人。

    2 回复  |  直到 10 年前
        1
  •  1
  •   Shravya Boggarapu    10 年前

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

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

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

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

    获得URL后,可以使用 fetchAssetsWithALAssetURLs:options: 作用

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

        2
  •  0
  •   Community Mohan Dere    9 年前

    没有一种简单的方法可以比较两张图片,如果你找到了一张图片,这将是一项艰巨的任务,相反,我们可以比较图片的元数据,我们可以将其写入图片,也可以从图片中读取。

    You can use this repo for that

    ALAssetLibrary还具有一些属性来访问图像元信息。如果你用这个搜索,你肯定会得到一些。

    Like this one

    推荐文章