Sunday 27 November 2016

Get list of all Videos

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.

Below chunk of code for retrieving list of video' assets from Photos application.

-(void) getAllVideoAssets{
    NSMutableArray *assets = [[NSMutableArray alloc] init];
    //Fetch all video assets from Photos
    PHFetchResult *assetResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];
    //get all assets
    for (PHAsset *asset in assetResults){
        NSLog(@"asset type = %zd", asset.mediaType);
        [assets addObject:asset];
    }
    self.allVideoslistArray = [[NSMutableArray alloc] init];
    //create an instance of PHImageManager
    PHImageManager *manager = [PHImageManager defaultManager];
    for(PHAsset *asset in assets){
        //block of code for represent video assets
        [manager requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
            if ([asset isKindOfClass:[AVURLAsset class]]) {
                NSURL *url = (NSURL *)[[(AVURLAsset *)asset URL] fileReferenceURL];
                UIImage *thumbnail = [self createThunbnailImage:url];
                [self.allVideoslistArray addObject:@{@"VideoUrl":url,@"ThumbnailImage":thumbnail, @"VideoAsset":asset}];
            }
        }];
    }//end for loop
}

No comments:

Post a Comment