如何从iOS上的Camera Roll中检索最近的照片? [英] How to retrieve the most recent photo from Camera Roll on iOS?

查看:187
本文介绍了如何从iOS上的Camera Roll中检索最近的照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力弄清楚如何在没有用户干预的情况下以编程方式检索相机胶卷中的最新照片。要清楚,我不想使用图像选择器,我希望应用程序在应用程序打开时自动抓取最新的照片。

I'm having a hard figuring out how to programmatically retrieve the most recent photo in the camera roll without user intervention. To be clear, I do not want to use an Image Picker, I want the app to automatically grab the newest photo when the app opens.

我知道这是可能的,因为我见过类似的应用程序,但我似乎无法找到任何信息。

I know this is possible because Ive seen a similar app do it, but I cant seem to find any info on it.

推荐答案

一种方法是使用AssetsLibrary并使用n - 1作为枚举的索引。

One way is to use AssetsLibrary and use n - 1 as the index for enumeration.

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                             usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                                 if (nil != group) {
                                     // be sure to filter the group so you only get photos
                                     [group setAssetsFilter:[ALAssetsFilter allPhotos]];

                                     if (group.numberOfAssets > 0) {
                                         [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:group.numberOfAssets - 1]
                                                                 options:0
                                                              usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                                                                  if (nil != result) {
                                                                      ALAssetRepresentation *repr = [result defaultRepresentation];
                                                                      // this is the most recent saved photo
                                                                      UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];
                                                                      // we only need the first (most recent) photo -- stop the enumeration
                                                                      *stop = YES;
                                                                  }
                                                              }];
                                     }
                                 }

                                 *stop = NO;
                             } failureBlock:^(NSError *error) {
                                 NSLog(@"error: %@", error);
                             }];

这篇关于如何从iOS上的Camera Roll中检索最近的照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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