当我在设备上运行程序时,AssetsLibrary 无法获取保存在相机胶卷中的图像

发布于 2024-10-06 08:56:43 字数 1822 浏览 5 评论 0原文

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

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

这个问题看起来与下面文章中讨论的问题相同: 资产库框架在 4.0 上无法正常工作和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.");
                         }];
}

您能告诉我您是否有任何想法来解决它吗?

I wrote a simple iOS program to get number of photo images which are saved in the camera roll by using 'Assets Library' framework provided in the SDK4.2.

The program worked well as I expected when I ran it on the iPhone simulator.
But, it didn't retrieve any images when I ran on the 'real' iPhone device (iPhone 3GS with iOS 4.2.1).

This problem looks like as same as the problem discussed in the below article:
Assets Library Framework not working correctly on 4.0 and 4.2

So, I added the "dispatch_async(dispatch_get_main_queue()..." function as below, But I couldn't solve the problem.

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

Could you please tell me if you have any ideas to solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗳卜坏 2024-10-13 08:56:43

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

I have 1 update:

To get error code, I modified the failureBlock of the enumerateGroupsWithTypes as below, and then reproduced the symptom again.

Then, the app returned the error code -3311 (ALAssetsLibraryAccessUserDeniedError).
However I didn't any operation to deny while my reproducing test.

What's the possible cause of the err#=-3311?

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                       usingBlock:assetGroupEnumerator
                     failureBlock: ^(NSError *error) {
                         NSLog(@"Failed");
        resultMsg = [NSString stringWithFormat:@"Failed: code=%d", [error code]];                     }];
墨落画卷 2024-10-13 08:56:43

奇怪的是,访问保存的照片时竟然要涉及位置服务。也许这与照片上的地理标记信息有关。无论如何,苹果表示使用 enumerateGroupsWithTypes:usingBlock:failureBlock:

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

It is strange that location services should be involved when accessing saved photos. Maybe it has to do with geo-tagging information on the photos. Anyways Apple says that enabling location services is required when using enumerateGroupsWithTypes:usingBlock:failureBlock:

Special Considerations
This method will fail with error ALAssetsLibraryAccessGloballyDeniedError if the user has not enabled Location Services (in Settings > General)."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文