15.7. Retrieving Assets from the Assets Library

Problem

You want to directly retrieve photos or videos from the photo library without the help of any built-in GUI components.

Solution

Use the Assets Library framework. Follow these steps:

  1. Allocate and initialize an object of type ALAssetsLibrary.

  2. Provide two block objects to the enumerateGroupsWithTypes:usingBlock:failureBlock: instance method of the Assets Library object. The first block will retrieve all the groups associated with the type that we passed to this method. The groups will be of type ALAssetsGroup. The second block returns an error in case of failure.

  3. Use the enumerateAssetsUsingBlock: instance method of each group object to enumerate the assets available in each group. This method takes a single parameter, a block that retrieves information on a single asset. The block that you pass as a parameter must accept three parameters, of which the first must be of type ALAsset.

  4. After retrieving the ALAsset objects available in each group, you can retrieve various properties of each asset, such as their type, available URLs, and so on. Retrieve these properties using the valueForProperty: instance method of each asset of type ALAsset. The return value of this method, depending on the property passed to it, could be NSDictionary, NSString, or any other object type. We will see a few common properties that we can retrieve from each asset soon.

  5. Invoke the defaultRepresentation instance method of each object of type ALAsset to retrieve ...

Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.