Monday, 19 December 2016

Get All Album and Assets

ALAssets library has deprecated in iOS9, and Apple introduce new framework  'Photos Framework' for retrieve assets for display and playback, edit image or video content, or work with collections of assets such as album, moments, and iCloud Shared Album.

Features:
1. Fetching objects and requesting changes
    Class's instance of Photos framework model retrieve assets(images, videos,  and Live Photos), collections of assets(albums or moments), and list of collections(such as album folder or moment clusters). There objects are read only, immutable and contain metadata only.
2. Change observing.
3. Support for Photos app features.
4. Assets and thumbnail loading and caching.
5. Assets content editing.

For details about Photos framework Click here

In this part of blog we will fetch all video assets from device(Photos app).
PHAssets:
All images, videos and live photos in Photos library represented by an instance of PHAssets.
 Instances of PHAsset are immutable.

PHFetchResult:
An ordered list of assets or collections returned from Photos fetch methods.
Photos provide all assets in the in a fetch result.

PHImageManager:
PHImageManager provide methos for retrieving and generating preview thumbnails and full-size image or video data associated with assets instance.

PHAssetCollection:
PHAssetCollection is collection of Photos and Videos assets , such as a moment, user-created album, smart album.

Get All Collection List:

1. Get list of smart albums
PHFetchResult *smartAlbum = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
    for (PHAssetCollection *collection in smartAlbum){
        NSLog(@"Title Album= %@",collection.localizedTitle);
        [tempArray addObject:collection];
    }


2. Get list of User created albums
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
    for (PHAssetCollection *collection in userAlbums){
        NSLog(@"Title Album= %@",collection.localizedTitle);
        [tempArray addObject:collection];
    }


Above code return all available list of collection of assets.

Now we can retrieve all assets from collection list.
here code for fetching all assets from collection list

 //initialize a mutable array for contain all assets
self.allImageAssetArray = [[NSMutableArray alloc] init];
 //Initialize an object of image manager
PHImageManager *manager = [PHImageManager defaultManager];
//fetch all image assets from collection list
    PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollectionIdentifier options:nil];
    NSLog(@"Total Count = %zd", assetsFetchResult.count);
    for (PHAsset *asset in assetsFetchResult){

    //Get image from Asset
        [manager requestImageForAsset:asset targetSize:thumbnailSize contentMode:PHImageContentModeAspectFit options:PHImageRequestOptionsResizeModeNone resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//add image to array
            [self.allImageAssetArray addObject:result];
        }];
    }
//end for loop

No comments:

Post a Comment