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:
Allocate and initialize an object of type
ALAssetsLibrary
.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 typeALAssetsGroup
. The second block returns an error in case of failure.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 typeALAsset
.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 thevalueForProperty:
instance method of each asset of typeALAsset
. The return value of this method, depending on the property passed to it, could beNSDictionary
,NSString
, or any other object type. We will see a few common properties that we can retrieve from each asset soon.Invoke the
defaultRepresentation
instance method of each object of typeALAsset
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.