- macOS 10.14+
- Xcode 10.0+
Starting in macOS 10.13, you can create Photos project extensions. This sample app shows you how to implement a slideshow extension that transitions between photos by zooming in to the region of interest (ROI) that’s algorithmically deemed most important. It demonstrates the computation of saliency based on an ROI’s weight and quality, and the process of subscribing to change notifications so your extension can respond to asset modifications.
Set Up the Project to Run Inside the Photos App
The sample app builds and runs in Xcode, but you open the macOS Photos app to access functionality. In the extension’s
Info file, designate the extension type by entering
slideshow in the field at
PHProject. You can add more categories to the information property list if you want your extension to appear in more categories in the Create menu.
From within the Photos app, access the Create categories by choosing File > Create or right-clicking any group of assets. Under the Slideshow category, you’ll see the app extension and can create a project to run in it.
Because the project extension runs inside the Photos.app, the sample emulates the grid layout of the user’s photo assets. Pressing the play button in the upper-right corner of the extension starts the slideshow.
Customize the Focus Rectangle of the Zoom Transition
The sample code project contains custom
Animator class handles transitions between photos in the slideshow. This sample’s
Animator asks an
Asset object for a rectangle to zoom in to. Photos identifies each human face it finds as a possible ROI, and the sample uses the bounding box of the most salient one as the preferred zoom rectangle. The code defines saliency of a
PHProject as the sum of its
quality values, then sorts the array of the photo’s regions by that value.
weight of an ROI represents the pervasiveness of the face in the project as a whole. The
quality score represents the quality of the ROI in the individual asset, based on factors such as sharpness, visibility, and prominence in the photo. Adding these two values is a heuristic for determining the face’s relative importance throughout a photo project. Objects that aren’t faces don’t qualify as ROI.
Respond to Asset Changes in the Project
Your app extension should monitor change notifications and respond to asset changes in the Photos library, like photos being added or removed.
Register for change observation as soon as the project begins or resumes. In the
PHProject protocol, the
resume methods provide points for your extension to begin monitoring changes.
When the project is complete, use the
finish protocol method to unregister from change observation.
Whenever something changes in the Photos library, the
photo method is called. When implementing this method, ask the
PHChange instance for details about changes to the object you’re interested in. When assets are added or removed, the sample project calls
updated to get an updated
PHProject instance, which you can use to refresh your UI.
Support Copy and Paste
If your extension handles the paste action, implement the
validate delegate method to handle pasteboard contents.