3

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?

Community
  • 1
  • 1
Hidehiko
  • 31
  • 1
  • 2

2 Answers2

0

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]];                     }];
Matty970
  • 21
  • 1
  • 2
  • 4
    Finally I got solution for this problem. I had to enable the "location service" to make this program works well. Here is the steps; (1) Install the above app to iPhone device. (2) Launch the [Settings] and go to [General] > [Location Services] > [] to turn it "ON". (3) Launch my app to run it. Then my app retrieved the photos from camera roll as I expected. Hope this helps, – Hidehiko Dec 17 '10 at 15:43
  • Ya it asks for location services but the behaviour is strange as why would an image fetch require location services. Is there any way i can do without turning my location services on and not being asked for location services to be allowed when i am fetching the photos in my app?? – manishnath Aug 18 '11 at 08:23
  • 1
    @chotachetan photos have location data associated. Accessing the photos through this mechanism allows you to also access the location data, therefore the user must permit your app to do this. – MattyG Oct 20 '11 at 10:24
0

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

Community
  • 1
  • 1
Jakob
  • 1