Search code examples
objective-cios9photosphotosframeworkassetslibrary

Find the size of photos and videos inside (Photo Library) iOS device using Photos Framework on iOS 8


Till iOS 7 we were using Assets Library Framework to get the total size of photos and videos in iOS device, look here : How to find total size for images and videos inside photo library in iOS

Can anyone please help me do the same using Photos Framework as Assets Library Framework is deprecated. Also how can I calculate the total size if there are large number of photos in a faster manner. You can see in this app : MobiShield for iPhone here if you go to More > Disk you can see that the developer is calculating the total photos size and videos size in 2 seconds. How can I achieve this ? In Objective - C.


Solution

  • Use these :

    - (NSUInteger)updateVideoCount
    {
        ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
        if (status==ALAuthorizationStatusDenied) {
            [self goToSettingsAlert];
        }
    videoCount = 0;
    totalVideoSize = 0;
    
    ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
    
    [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        [group setAssetsFilter:[ALAssetsFilter allVideos]];
        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
            if (asset)
            {
                NSString *type = [asset  valueForProperty:ALAssetPropertyType];
                if ([type isEqualToString:ALAssetTypeVideo])
                {
                    videoCount++;
    
                    ALAssetRepresentation *rep = [asset defaultRepresentation];
                    totalVideoSize += rep.size;
                }
    
            }
            else
            {
    
            }
        }];
        if(group==nil)
        {
            [self loadTable];
            descTable.hidden = NO;
            [descTable reloadData];
        }
    } failureBlock:^(NSError *error) {
    }];
    
    return 0;
    

    }

    - (NSUInteger)updatePictureCount
    {
        photoCount = 0;
        totalPictureSize = 0;
    
        ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
    
        [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            [group setAssetsFilter:[ALAssetsFilter allPhotos]];
            [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
                if (asset)
                {
                    NSString *type = [asset  valueForProperty:ALAssetPropertyType];
                    if ([type isEqualToString:ALAssetTypePhoto])
                    {
                        photoCount++;
    
                        ALAssetRepresentation *rep = [asset defaultRepresentation];
                        totalPictureSize += rep.size;
                    }
    
                }
    
                else
                {
    
                }
            }];
            if(group==nil)
            {
    
                [self loadTable];
                descTable.hidden = NO;
                [descTable reloadData];
            }
        } failureBlock:^(NSError *error) {
    
        }];
    
        return 0;
    }