ALAssetsLibrary is deprecated. Now What?

When developing applications for iOS9.x you will get a warning about ALAssetsLibrary is deprecated and you should not use it. You are using Assets Library framework for some years, you learned everything about it but lets face it; ALAssetsLibrary is old. And now it left short when we talk about newer and modern iOS Photos features. Luckily, Apple gave us the Photos Framework with iOS 8. You can develop applications that using user’s photos with new Photos Framework.

Photos Library provides modern API’s to us for;

  • Request and check authorization from user
  • Retrieving user’s Albums
  • Retrieving photos, videos and metadatas
  • Editing and updating Albums
  • Editing and updating images and videos
  • Listening and reacting to changes
  • Caching images

Before starting writing some code, let’s check some important assets of Photos Library.

PHPhotoLibrary: The shared PHPhotoLibrary object represents the user’s Photos library—the entire set of assets and collections managed by the Photos app, including objects stored on the local device and (if enabled) in iCloud Photos.

PHAsset: A PHAsset object represents an image or video file that appears in the Photos app, including iCloud Photos content.

PHAssetCollection: A PHAssetCollection object represents a collection of photo or video assets.

PHCollectionList: A PHCollectionList object represents a group of asset collections.

PHImageManager: The shared PHImageManager object provides methods for loading image or video data associated with a PHAsset object.

PHFetchOptions: You use a PHFetchOptions object to specify options when using class methods on the PHAsset, PHCollection, PHAssetCollection, and PHCollectionList classes to retrieve photo entities.

PHFetchResult: A PHFetchResult object is a container for an ordered list of photo entity objects.

PHChange: Photos provides PHChange objects to notify your app of changes to the assets and collections managed by the Photos app.

Using Photos Framework

Before we start to use Photos Framework API’s, we need to have permission of using photos/videos. If user does not give permission, like other privacy permission required services of iOS, we can not reach to the photos in device.

iOS can ask user when we request to access users photos first time. Also we can use  PHPhotoLibrary.authorizationStatus()  enum for checking user’s authorization status and  PHPhotoLibrary.requestAuthorization(handler)  method for requesting authorization from user manually.

If user denied to give authorization or use of Photos is restricted, you can show an alert to user about this application needs permission for working with photos.

After getting the permission, you can use Photos Framework with your application flow. Let’s take a look to some important actions of Photos Framework.

Getting List of User’s Albums

You can use  PHAssetCollection.fetchAssetCollections(type:subtype:options:)  method for getting albums of the user.

First parameter is  PHAssetCollectionType enum, which allows you to filter the type of the collection.   PHAssetCollectionType  contains  .Album , .SmartAlbum  and  .Moment.

Second parameter is  PHAssetCollectionSubtype enum, which allows you filter sub-type of the collection.  PHAssetCollectionSubtype  contains;

Third and last parameter is PHFetchOptions , which allows you to sort, filter or limit requests.

After requesting collections, you can use it like an array. You can get items via subscript, enumerating or any other way that arrays support.

Getting List of the Photos

After getting collections of user, you can get photos in it or you can request all assets.

We sorted user’s all photos with “creationDate”. This is looking like a magic string but not 🙂 You can find a list of sort and predicate keys here.


Leave a Reply

Your email address will not be published. Required fields are marked *