How do you get Photos Framework assets by date?

I’m overwhelmed and don’t know where to start. I do know that I need to use Photos Framework, but from there I am lost. I have looked at iOS 8 by Tutorials. It’s for Swift 1.2. How much has changed?

Basically I want to be access the assets in the photo library, but want to do it by date and not “all”. I’ve looked at the code sample at Example app using Photos framework, but how to grab assets by date has me stuck. Google has not been my friend; can anyone else help?

Humbly,
Stephanie

1 Like

Take a look at MasterViewController.swift in the code sample you referenced. Here is a piece of code from the viewDidLoad method.

let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: “creationDate”, ascending: true)]
allPhotos = PHAsset.fetchAssets(with: allPhotosOptions)

In this case, allPhotosOptions (of type PHFetchOptions) is only used to sort the the photos by creation date. In addition to the sortDescriptors property, PHFetchOptions also has a predicate property. Using a predicate lets you select photos based on a bunch of photo attributes. Try something like this:

allPhotosOptions.predicate = NSPredicate(format: “creationDate = %@”, selectedDate as CVarArg)

I’m not where I can test this out, but that should give you something to get started with.

Have fun!