iOS.Lover
iOS.Lover

Reputation: 6051

Detect Photo Library is empty

I am trying to load and show the last photo from photo library (Camera roll) in UIImageView, everything works fine ! except one thing ! if there is no image available in library then app crashes !! here is my code :

 -(void)importLastImage {

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        // Within the group enumeration block, filter to enumerate just photos.
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];

        // Chooses the photo at the last index
        [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]
                                options:0
                             usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

                                 // The end of the enumeration is signaled by asset == nil.
                                 if (alAsset) {
                                     ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                                     latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]];

                                    _lastImage.image = latestPhoto;

                                 }else {

                           //no image found !

                                 }
       }];
    }
                         failureBlock: ^(NSError *error) {
                             // Typically you should handle an error more gracefully than this.
                             NSLog(@"No groups");

                             UIAlertView *alert  = [[UIAlertView alloc]initWithTitle:@"ERROR" message:@"No Image found" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
                             [alert show];

}];

}

Upvotes: 0

Views: 166

Answers (1)

AMI289
AMI289

Reputation: 1108

I decided to post here a code snippet that you could just copy-paste, that contains the suggestion from my comment above.
Sorry in advance if my english isn't clear enough (I'm not an native english speaker).

Your crashes is due to that line of code
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]

The problem is, as you've said, when there is no images available in the library, numberOfAssets returns 0,
And since you are create index with numberOfAssets - 1, you are basically trying to create a negative index, which crashes your program.

I've simply added an if statement, to check 'if' numberOfAssets (meaning it's not 0),
Only if so, execute the below enumeration, therefore, prevent any case of negative index.

Anyways, here you go mate:

-(void)importLastImage {

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        // Within the group enumeration block, filter to enumerate just photos.
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];

        if([group numberOfAssets]) {
            // Chooses the photo at the last index
            [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]
                                    options:0
                                 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

                                     // The end of the enumeration is signaled by asset == nil.
                                     if (alAsset) {
                                         ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                                         latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]];

                                        _lastImage.image = latestPhoto;

                                     }else {

                               //no image found !

                                     }
           }];
        } else {
            NSLog(@"No images in photo library");
        }
    }
                         failureBlock: ^(NSError *error) {
                             // Typically you should handle an error more gracefully than this.
                             NSLog(@"No groups");

                             UIAlertView *alert  = [[UIAlertView alloc]initWithTitle:@"ERROR" message:@"No Image found" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
                             [alert show];

    }];

}

Upvotes: 1

Related Questions