Obj-C检查照片库中是否已存在图像 [英] Obj-C Check if image already exists in Photos gallery

查看:119
本文介绍了Obj-C检查照片库中是否已存在图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app I have to implement save image feature. I have managed saving like this:

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

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

Unfortunately I have to check if file already exists to prevent redundant saving. Also this is necessary because saving multiple times the same image don't override one file, but it creates copy of it...

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

Do you know how to solve that problem?

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

According to Shravya Boggarapu answer I'm storing assetUrl in my NSUserDefaults. Complete code:

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

希望它会对某人有所帮助。

Hope it will help somebody.

推荐答案

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

I have a solution but not one that applies to every situation.

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

The thing about saving image to camera roll is that there is an assetURL created when you add the image to camera roll. Also, this asset URL is new every time, so if you save to camera roll, it will create a copy. The name of the image is also not retained.

如果图像首先通过您的应用添加到相机胶卷,那么您可以将assetsURL存储为零件图像信息。

If the image is added to the camera roll through your app in the first place, then you can store the assetURL as part of image information.

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

In my app, a dictionary is maintained for every image containing some critical info. This includes the assetURL of the image if it is saved to camera roll.

获得URL后,可以使用 fetchAssetsWithALAssetURLs检查其存在性:options: function。

Once you have the URL, you can check its existence by using fetchAssetsWithALAssetURLs:options: function.

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

If the URL is nil or the asset when fetched is nil, it means that the image does not exist in the camera roll. So you can add anew.

这篇关于Obj-C检查照片库中是否已存在图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆