AssetsLibrary does not receive images stored in the camera roll when I run the program on the device

I wrote a simple iOS program to get the number of photos that are saved in the camera frame using the Assets framework provided in SDK4.2.

The program worked well as I expected when I ran it on an iPhone simulator. But he didn’t get any images when I launched the “real” iPhone device (iPhone 3GS with iOS 4.2.1).

This problem looks the same as the problem discussed in the following article: The structure of the resource library is not working correctly 4.0 and 4.2

So, I added the dispatch_async (dispatch_get_main_queue () ... "function, as shown below, but I could not 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.");
                         }];
}

, - ?

+3
2

1 :

, faultBlock enumerateGroupsWithTypes, , .

-3311 (ALAssetsLibraryAccessUserDeniedError). , , , .

# = - 3311?

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                       usingBlock:assetGroupEnumerator
                     failureBlock: ^(NSError *error) {
                         NSLog(@"Failed");
        resultMsg = [NSString stringWithFormat:@"Failed: code=%d", [error code]];                     }];
0

, . , . Apple , enumerateGroupsWithTypes:usingBlock:failureBlock:

ALAssetsLibraryAccessGloballyDeniedError, ( > ). "

0

Source: https://habr.com/ru/post/1779239/


All Articles