开发者

AssetsLibrary does not get images saved in the Camera roll when I run the program on the device

开发者 https://www.devze.com 2023-01-29 07:10 出处:网络
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.

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?


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


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)."

0

精彩评论

暂无评论...
验证码 换一张
取 消