objective-c - 如何从 iOS 上的相机胶卷中检索最新照片?

标签 objective-c ios

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

我知道这是可能的,因为我见过一个类似的应用程序这样做,但我似乎找不到任何关于它的信息。

最佳答案

一种方式是使用AssetsLibrary,以n-1作为索引进行枚举。

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

关于objective-c - 如何从 iOS 上的相机胶卷中检索最新照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10200553/

相关文章:

objective-c - iMac - 缺少 x86 版本的 GameKit

objective-c - Xcode 在调试器中暂停时具有错误的调试值

ios - didReceiveMemoryWarning,viewDidUnload

ios - "Thread 1: signal SIGTERM"在带有 iOS 的 xcode 中是什么意思?

ios - 以编程方式添加一个新的 UIView,其中包含不同的数据

iphone - 带有 JSON 帮助的 UITableView

ios - 使用 UIWebView 快速滚动

ios - 使用 JSONDecoder 解码带有空对象的 JSON

ios - 如何在 Swift 中检查实例的类型?

ios - 转换不适用于 IOS