Tìm hiểu photos framework
Introduction Before ios8, to use media datatype, ios use Asset Library framework, by time camera app and photo app update more features, one of these is filter by moments. From ios8 apple introduction a new framework and will replace asset library in the future, provide many modern features to ...
Introduction
Before ios8, to use media datatype, ios use Asset Library framework, by time camera app and photo app update more features, one of these is filter by moments. From ios8 apple introduction a new framework and will replace asset library in the future, provide many modern features to couple link to device's photo library.
Photo framework features
Model data
- Structure device's library
- Photo and video assets, moments, albums, folder,etc.
- Read-only, thread-safe
- Contain:
-
PHAsset:
- Represents a single asset in the user’s photo library, providing the metadata for that asset.
- Information: Media type, creation date, location, favorite.
-
PHAssetCollection:
- Groups of assets. A single asset collection can be an album or a moment in the photo library, as well as one of the special “smart albums.” These include collections of all videos, recently added items, user favorites, all burst photos, and more. PHAssetCollection is a subclass of PHCollection.
- Information:type, title, start and end date.
-
PHCollectionList:
- Represents a group of PHCollections. Since it is a PHCollection itself, a collection list can contain other collection lists, allowing for complex hierarchies of collections. In practice, this can be seen in the Moments tab in the Photos.app: Asset — Moment — Moment Cluster — Moment Year.
- Information:type,title,start and end date.
- Some model data's features:
- Fetch model objects:
-
Fetch via class methods on model object.
-
Fetch all video assets:
[PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil]
-
Fetch all moments:
[PHAssetCollection fetchMomentsWithOptions:nil]
-
Use options to filter and sort
-
Change model data:
-
Support for user actions: favortie a photo, add to an album
-
Model object are read-only, cannot mutate directly
-
Create new Model object:
// Create via creation request request = [PHAssetChangeRequest creationRequestForAssetFromImage:image] placeholder = [request placeholderForCreatedAsset]
-
Change request class for each model class
PHAssetChangeRequest PHAssetCollectionChangeRequest PHCollectionListChangeRequest
-
Provide model object–specific APIs
setCreationDate: setFavorite: exp: -(void)toggleFavorite:(PHAsset *)asset { // Changes must be performed in a change block [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ // Create a change request for the asset PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest changeRequestForAsset:asset]; [changeRequest setFavorite:![asset isFavorite]]; } completionHandler:^(BOOL success, NSError *error) { ... }];
-
-
Handle model changes
Lots of sources change:
+ Your application, other applications.
+ iCloud Photo Library, iCloud Photo Sharing, My Photo Stream.
Use PHChange to registed observers exp: - (void)photoLibraryDidChange:(PHChange *)change {
// re-dispatch to main queue dispatch_async(dispatch_get_main_queue(), ^{ // get change details PHFetchResultChangeDetails *changeDetails = [change changeDetailsForFetchResult:self.assets]; // get the updated fetch results if (changeDetails) { self.assets = [changeDetails fetchResultAfterChanges]; ... }
Summary:
If compare with asset library, photo framework have many advance features evolve query data and notification. But photo framework only active in ios8. Below this is source code. Thanks for watching