3

我编写了一个简单的 iOS 程序,通过使用 SDK4.2 中提供的“资产库”框架来获取保存在相机胶卷中的照片图像数量。

当我在 iPhone 模拟器上运行该程序时,该程序运行良好。但是,当我在“真正的”iPhone 设备(带有 iOS 4.2.1 的 iPhone 3GS)上运行时,它没有检索到任何图像。

此问题看起来与以下文章中讨论的问题相同: Assets Library Framework not working correct on 4.0 and 4.2

所以,我添加了“dispatch_async(dispatch_get_main_queue()...”函数如下,但我无法解决问题。

- (void)viewDidLoad {
    [super viewDidLoad];

    NSMutableArray assets = [[NSMutableArray array] retain]; // Prepare array to have retrieved images by Assets Library.

    void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
        if(asset != NULL) {
            [assets addObject:asset]; 
            dispatch_async(dispatch_get_main_queue(), ^{

                // show number of retrieved images saved in the Camera role.
                // The [assets count] returns always 0 when I run this program on iPhone device although it worked OK on the simulator.
                NSLog(@"%i", [assets count]);
            });
        }
    };

    void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
        }
    };

    // Create instance of the Assets Library.
    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos // Retrieve the images saved in the Camera role.
                           usingBlock:assetGroupEnumerator
                         failureBlock: ^(NSError *error) {
                             NSLog(@"Failed.");
                         }];
}

你能告诉我你是否有解决它的想法吗?

4

2 回答 2

0

访问保存的照片时应该涉及位置服务,这很奇怪。也许它与照片上的地理标记信息有关。无论如何,Apple表示使用时需要启用位置服务enumerateGroupsWithTypes:usingBlock:failureBlock:

特殊注意事项ALAssetsLibraryAccessGloballyDeniedError如果用户未启用定位服务(在设置 > 常规中), 此方法将失败并出现错误。”

于 2011-09-03T18:31:25.390 回答
0

我有 1 个更新:

为了获取错误代码,我修改了 enumerateGroupsWithTypes 的 failureBlock 如下,然后再次重现了症状。

然后,该应用返回错误代码 -3311 (ALAssetsLibraryAccessUserDeniedError)。但是我在复制测试时没有任何操作来否认。

err#=-3311 的可能原因是什么?

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                       usingBlock:assetGroupEnumerator
                     failureBlock: ^(NSError *error) {
                         NSLog(@"Failed");
        resultMsg = [NSString stringWithFormat:@"Failed: code=%d", [error code]];                     }];
于 2010-12-10T10:02:12.693 回答