Loading videos with predicates — iOS — Swift

Himal Madhushan
3 min readSep 26, 2017

--

Here is an article for those who are trying to load videos with predicates in Photos Kit.

First create a global variable that can take fetch results

var assetsFetchResult = PHFetchResult<PHAsset>()

Then we create variable from PHFetchOptions(). This will contain all our predicates and sortDescriptors.

let option = PHFetchOptions()

I was working on an application that loads videos by date. So I will use that as the predicate and sort by creationDate.

Here is the code that I used to setup my PHFetchOptions()

currentDate is a global variable of Date()

let option = PHFetchOptions()var calendar = Calendar.currentcalendar.timeZone = TimeZone.currentlet predicate = NSPredicate(format: "creationDate > %@ AND creationDate < %@ AND mediaType == %i", currentDate.startOfDay as NSDate, currentDate.endOfDay as NSDate, PHAssetMediaType.video.rawValue)option.predicate = predicatelet sort = NSSortDescriptor(key: "creationDate", ascending: true)option.sortDescriptors = [sort]

See the predicate options you have..

😯 If you wonder how I got the currentDate.startOfDay, Don’t worry… I wrote a simple extension for that and here it is…

extension Date {
var startOfDay : Date {
let calendar = Calendar.current let unitFlags = Set<Calendar.Component>([.year, .month, .day]) let components = calendar.dateComponents(unitFlags, from: self) return calendar.date(from: components)! }}

Then we are good to go.. Now we fetch videos with PHAssetCollection that uses to group videos.

let videoSmartAlbumsFetchResult = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumVideos, options: nil)

It returns PHAssetCollection with smartAlbum results. Then we grab that collection and derive the data out of it from the code below.

if videoSmartAlbumsFetchResult.count > 0 {  let videoSmartAlbum = videoSmartAlbumsFetchResult[0]  assetsFetchResult = PHAsset.fetchAssets(in: videoSmartAlbum, options: option)  PHPhotoLibrary.shared().register(self)}

You might wonder what does this line do… 😳

PHPhotoLibrary.shared().register(self)

Wanna know about it? please read this article…

Now let’s look at how you can read the videos that are in assetsFetchResult .

It’s just a for loop to loop trough the assetsFetchResult array.

for i in 0..<assetsFetchResult.count { let asset = self.assetsFetchResult[i]}

Huh.. Pretty simple.. isn’t it..

Now we have our asset from PHAsset . Fell free to do anything with it you like..

So the complete method is here..

var assetsFetchResult = PHFetchResult<PHAsset>()private func loadVideos() { let option = PHFetchOptions() var calendar = Calendar.current calendar.timeZone = TimeZone.current let predicate = NSPredicate(format: "creationDate > %@ AND creationDate < %@ AND mediaType == %i", currentDate.startOfDay as NSDate, currentDate.endOfDay as NSDate, PHAssetMediaType.video.rawValue) option.predicate = predicate let sort = NSSortDescriptor(key: "creationDate", ascending: true) option.sortDescriptors = [sort] let videoSmartAlbumsFetchResult = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumVideos, options: nil) if videoSmartAlbumsFetchResult.count > 0 {  let videoSmartAlbum = videoSmartAlbumsFetchResult[0]  assetsFetchResult = PHAsset.fetchAssets(in: videoSmartAlbum, options: option)  PHPhotoLibrary.shared().register(self) }}

Enjoy. 😊

--

--