ios app crashes for some users when using ALAssetsLibrary to retrieve images in a UICollectionView

454 views Asked by At

I have an ios app, I have accessing images from the gallery using ALAssetLibrary to populate UICollectionView. It is working fine for test users in India(My home country) but it is not working on the devices of our client in USA. It crashes when client opens up the collection view with following error:


Fatal Exception: NSInvalidArgumentException
*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil

Thread : Fatal Exception: NSInvalidArgumentException
    0  CoreFoundation                 0x182ef9900 __exceptionPreprocess
    1  libobjc.A.dylib                0x182567f80 objc_exception_throw
    2  CoreFoundation                 0x182de3134       CFStringConvertNSStringEncodingToEncoding
    3  QuikCom                        0x10009e594 __27-[ImageSelector loadAssets]_block_invoke_2 (ImageSelector.m:65)
    4  AssetsLibrary                  0x18c74ba4c __62-[ALAssetsGroup _enumerateAssetsAtIndexes:options:usingBlock:]_block_invoke147
    5  CoreFoundation                 0x182df0370 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke
    6  CoreFoundation                 0x182df0268 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:]
    7  AssetsLibrary                  0x18c74b520 -[ALAssetsGroup _enumerateAssetsAtIndexes:options:usingBlock:]
    8  QuikCom                        0x10009e4a4 __27-[ImageSelector loadAssets]_block_invoke (ImageSelector.m:60)
    9  AssetsLibrary                  0x18c74d8e4 __68-[ALAssetsLibrary enumerateGroupsWithTypes:usingBlock:failureBlock:]_block_invoke_3
    10 CoreFoundation                 0x182df0370 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke
    11 CoreFoundation                 0x182df01e0 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:]
    12 AssetsLibrary                  0x18c74d83c __68-[ALAssetsLibrary enumerateGroupsWithTypes:usingBlock:failureBlock:]_block_invoke_2
    13 libdispatch.dylib              0x18294d630 _dispatch_call_block_and_release
    14 libdispatch.dylib              0x18294d5f0 _dispatch_client_callout
    15 libdispatch.dylib              0x182952cf8 _dispatch_main_queue_callback_4CF
    16 CoreFoundation                 0x182eb0bb0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
    17 CoreFoundation                 0x182eaea18 __CFRunLoopRun
    18 CoreFoundation                 0x182ddd680 CFRunLoopRunSpecific
    19 GraphicsServices               0x1842ec088 GSEventRunModal
    20 UIKit                          0x187c54d90 UIApplicationMain
    21 QuikCom                        0x1000a1258 main (main.m:14)
    22 libdispatch.dylib              0x18297e8b8 (Missing)

Then Put check for nil values. The code to fetch images is as follows:


-(void)loadAssets{

    __block ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        if (group == nil) {
            return;
        }

        [group setAssetsFilter:[ALAssetsFilter allPhotos]];

        if ( group != nil && [group numberOfAssets] > 0) {
            [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

                if (alAsset) {

                    ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                    UIImage *latestPhotoThumbnail =  [UIImage imageWithCGImage:[alAsset thumbnail]];

                    if (representation != nil &&representation.url != nil && latestPhotoThumbnail != NULL) {
                        [urlArray addObject:representation.url];
                        [thumbsArr addObject:latestPhotoThumbnail];
                        representation = nil;
                        latestPhotoThumbnail = nil;
                    }

                }else{
                    library = nil;
                    [_collectionView reloadData];
                }
            }];
        }

        [_collectionView reloadData];

    } failureBlock: ^(NSError *error) {

        //NSLog(@"No groups: %@",error);
    }];
}

After that the app did not crash but the collection view is rendering with empty cells without image thumbnails. I do not have any idea what I am doing wrong.

0

There are 0 answers