6
\$\begingroup\$

This function is supposed to return a map of all unique local identifiers of images that are on the iPhone using the IOS Photos Framework.

(it is a map so nSnull or (any object can be mapped to it) and retrieved in O(1))

I was never sure if in this way I am iterating every photos or am i missing something.

 - (NSDictionary*) getAllPhotos
        {
            PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
            if (status == PHAuthorizationStatusAuthorized)
            {
                //initialize empty local state
                __block NSMutableDictionary* currentImageState = [NSMutableDictionary dictionaryWithCapacity:0];
                @autoreleasepool
                {  
                    PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
                    userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];

                    //fetch all the albums

                    PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];
                    PHFetchOptions *onlyImagesOptions = [PHFetchOptions new];
                    onlyImagesOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage];
                    onlyImagesOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];  
                    [userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) { 
                    PHFetchResult *collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:onlyImagesOptions];

                    [collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
                            [currentImageState setObject:[NSNull null] forKey:asset.localIdentifier];
                        }];
                    }];

                    PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];

                    [allPhotosResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
                         [currentImageState setObject:[NSNull null] forKey:asset.localIdentifier];
                     }];
                }
                return [currentImageState copy];
            }
            return 0;
        }
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.