当我在设备上运行程序时,AssetsLibrary 无法获取保存在相机胶卷中的图像
我编写了一个简单的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有 1 个更新:
为了获取错误代码,我修改了 enumerateGroupsWithTypes 的 failureBlock 如下,然后再次重现症状。
然后,应用程序返回错误代码 -3311 (ALAssetsLibraryAccessUserDeniedError)。
不过,在我的复制测试中,我没有进行任何操作来拒绝。
err#=-3311 的可能原因是什么?
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?
奇怪的是,访问保存的照片时竟然要涉及位置服务。也许这与照片上的地理标记信息有关。无论如何,苹果表示使用
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)."