检查图像是否已经在图库中并检索它 [英] Check if an image is already in gallery and retrieve it

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

问题描述

我需要知道图像是否已经在 iOS 画廊中.我正在开发一个消息客户端应用程序,在我的聊天列表中,当我点击图像缩略图时,我可以下载传入消息的图像/视频.

I need to know if an image is already in the iOS gallery. I am developing a messaging client app and, in my chat list, I am able to download images/videos of incoming messages when I tap into the image thumbnail.

问题是,当我点击缩略图时,我将该图像下载并保存到 iOS 照片库中,但是如果我再次点击我不想再次下载并保存它,我想从照片库.

The issue is, when I tap in the thumbnail, I download and save that image into the iOS photo gallery, but if I tap again I don't want to download and save it again, I want to retrieve it from the photo gallery.

继续,我想在照片库中查找图像并检索它.

Resuming, I want to look for the image in the photo gallery and retrieve it.

这是我使用 ALAssetsLibrary 将图像保存在我的自定义相册中的代码:

Here is my code to save the image in my custom photo album using ALAssetsLibrary:

[self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:
    ^(NSURL* assetURL, NSError* error)
    {
        if (error!=nil) {
        completionBlock(error);

        return;
        }

        //add the asset to the custom photo album
        [self addAssetURL: assetURL
                  toAlbum:albumName
      withCompletionBlock:completionBlock];
    }
 ];

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
    __block BOOL albumWasFound = NO;

    [self enumerateGroupsWithTypes:ALAssetsGroupAlbum
                        usingBlock:
        ^(ALAssetsGroup *group, BOOL *stop)
        {
            if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
                albumWasFound = YES;
                [self assetForURL: assetURL
                      resultBlock:
                    ^(ALAsset *asset)
                    {
                        //add photo to the target album
                        [group addAsset: asset];
                        completionBlock(nil);

                    } failureBlock: completionBlock
                ];

                return;
            }

            if (group==nil && albumWasFound==NO) {
            //Target album does not exist, create it
                __weak ALAssetsLibrary* weakSelf = self;

                [self addAssetsGroupAlbumWithName:albumName
                                      resultBlock:
                    ^(ALAssetsGroup *group)
                    {
                        [weakSelf assetForURL: assetURL
                                  resultBlock:
                            ^(ALAsset *asset)
                            {
                                //add photo to the newly created album
                                [group addAsset: asset];
                                completionBlock(nil);

                            } failureBlock: completionBlock
                         ];

                    } failureBlock: completionBlock
                ];
                return;
            }

        } failureBlock: completionBlock
    ];
}

也许使用 assetURL 我可以做到,但我一直在搜索 Apple 的文档,但没有看到任何内容.

Maybe with the assetURL I could do it, but I have been searching in Apple's documentation and I didn't see anything.

谢谢!!

推荐答案

您可以使用完成块在保存过程后获取资产 URL.您可以使用此资产 URL 检索保存的图像.

You can get Asset URL after the save process using completion block. You can retrieve the saved image using this Asset URL.

很遗憾,您无法在 Photolibrary 中检查文件是否存在.背后的原因是,当您保存到 Photolibrary 时,系统会自动在图像中嵌入许多额外的信息.例如位置、时间等.因此对于相同的图像,二进制内容会有所不同.

Unfortunately, you can't check file exist in Photolibrary. The reason behind is, the system will automatically embed many extra information with the image while you save in to Photolibrary. example like location, time etc.. So the binary content will be different for same images.

因此,您只有一种解决方案.将图像保存到文档目录中,并使用CRCMD5 等算法使文件存在.

So, you have only one solution. Save the images into document directory and do a file exist using algorithms like CRC, MD5 etc..

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

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