Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch...

150
© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. #WWDC14 Introducing the Photos Frameworks Session 511 Adam Swift iOS Photos Frameworks Media

Transcript of Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch...

Page 1: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

#WWDC14

Introducing the Photos Frameworks

Session 511 Adam Swift iOS Photos Frameworks

Media

Page 2: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Introducing PhotoKit

Photos • Access photos and videos from photo library

• Create a full-featured app like built-in Photos app

Photos UI • Photo editing app extensions

Page 3: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

What You Will Learn

Photos framework • Fetch and manipulate photo library model data

• Handle external changes

• Retrieve and edit photo/video content

PhotosUI Framework • How to build photo/video editing extensions

Page 4: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Photos Framework

Page 5: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

New Objective-C framework on iOS

First-class citizen • Custom image picker

• Full-featured photo library browser and editor

Introducing the Photos Framework

Page 6: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

New Objective-C framework on iOS

Make your app a first-class citizen of user’s Photo Library • Custom image picker

• Full featured photo

Used by built-in Photos and Camera app

Intended to supersede ALAssetsLibrary

New Objective-C framework on iOS

First-class citizen • Custom image picker

• Full-featured photo library browser and editor

Introducing the Photos Framework

Page 7: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

What Does It Provide?

Access photo and video assets, albums, and moments

Add, remove, modify assets and albums

Edit photo/video content and metadata

Page 8: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Photos API Overview

Model data • Fetch model objects

• Change model data

• Handle model changes

Image and video content • Retrieve image/video content

• Edit content

Page 9: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Data

Page 10: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Represent the structure of your library • Photo and video assets, moments, albums, folders, etc.

Read-only

Thread-safe

Page 11: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Page 12: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Assets

Page 13: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Assets

Page 14: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Assets

Page 15: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Assets

Page 16: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Asset Collections

Assets

Page 17: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Asset Collections

Assets

Page 18: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Asset Collections

Assets

Page 19: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Asset Collections

Assets

Page 20: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Asset Collections

Assets

Collection Lists

Page 21: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Asset Collections

Assets

Collection Lists

Page 22: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Model Objects

Asset Collections

Assets

Collection Lists

Page 23: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Assets

Photos and videos

Page 24: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Assets

Photos and videos

PHAsset • Media type

• Creation date

• Location

• Favorite

Page 25: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Asset Collection

Ordered collection of assets

Albums, moments, and smart albums

Page 26: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Asset Collection

Ordered collection of assets

Albums, moments, and smart albums

PHAssetCollection • Type

• Title

• Start and end date

Page 27: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Collection List

Ordered collection of collections • Both asset collections and collection lists

Folder, moment year

Page 28: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Collection List

Ordered collection of collections • Both asset collections and collection lists

Folder, moment year

PHCollectionList • Type

• Title

• Start and end date

Page 29: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetching Model Objects

Fetch via class methods on model object

Fetch all video assets [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil]

Page 30: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetching Model Objects

Fetch via class methods on model object

Fetch all video assets [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil]

Fetch all moments [PHAssetCollection fetchMomentsWithOptions:nil]

Page 31: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetching 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

Page 32: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetching Collection Contents

Collections do not cache their contents

Fetch contents via class methods

Page 33: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetching Collection Contents

Collections do not cache their contents

Fetch contents via class methods

Fetch assets in an asset collection [PHAsset fetchAssetsInAssetCollection:myAlbum options:nil]

Page 34: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Transient Collections

Runtime-only transient collection • Search results, user selection

Page 35: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Transient Collections

Runtime-only transient collection • Search results, user selection

Interchangeable with regular collections • Fetch contents

• Reuse your view controllers, etc.

!

Page 36: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Transient Collections

Create a transient asset collection [PHAssetCollection transientAssetCollectionWithAssets:assets title:title]

Page 37: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetch Results

Want synchronous, fast results

Results of a fetch can be very large • Don’t need all objects in memory at once

• Work in batches

Page 38: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetch Results

Results returned in a PHFetchResult • Tracks IDs of the full result set

• Vends fully realized objects

• Similar API to NSArray

Page 39: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetch Results

Results returned in a PHFetchResult • Tracks IDs of the full result set

• Vends fully realized objects

• Similar API to NSArray

Fetch result

Page 40: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetch Results

Results returned in a PHFetchResult • Tracks IDs of the full result set

• Vends fully realized objects

• Similar API to NSArray

assets[n]Fetch result

Page 41: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetch Results

Results returned in a PHFetchResult • Tracks IDs of the full result set

• Vends fully realized objects

• Similar API to NSArray

assets[n]Fetch result

Page 42: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Making Model Changes

Support for user actions • Favorite a photo

• Add to an album

Model objects are read-only, can’t mutate directly

Page 43: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Requests API

Change request classes

Create in a change request block

Applied asynchronously out of process

Page 44: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change request class for each model class PHAssetChangeRequest PHAssetCollectionChangeRequest PHCollectionListChangeRequest

Provide model object–specific APIs setCreationDate: setFavorite:

Change Request Classes

Page 45: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Not subclasses of model classes

Separates thread-safe, immutable model objects and mutations

Only valid within a change request block

Change Request Classes

Page 46: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Request Example

- (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) { ... }];

Page 47: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Request Example

- (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) { ... }];

Page 48: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Request Example

- (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) { ... }];

Page 49: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Request Example

- (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) { ... }];

Page 50: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Request Example

- (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) { ... }];

Page 51: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Creating New Model Objects

Create via creation request request = [PHAssetChangeRequest creationRequestForAssetFromImage:image]

Page 52: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Creating New Model Objects

Create via creation request request = [PHAssetChangeRequest creationRequestForAssetFromImage:image]

Placeholder objects placeholder = [request placeholderForCreatedAsset]

• Reference to a new, unsaved object

• Add to collections

• Can provide unique, persistent localIdentifier

Page 53: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Whither Changes?

Changes are done when completion handler invoked

Model objects aren’t refreshed

Side effects and external changes!

Page 54: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Handling Model Changes

Lots of sources of change • Your application, other applications

• iCloud Photo Library, iCloud Photo Sharing, My Photo Stream

Page 55: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Handling Model Changes

Lots of sources of change • Your application, other applications

• iCloud Photo Library, iCloud Photo Sharing, My Photo Stream

Page 56: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Notification

We publish a PHChange to registered observers • Delivered on a background queue

Details on updated and deleted objects

Page 57: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Notification

We publish a PHChange to registered observers • Delivered on a background queue

Details on updated and deleted objects

Updated fetch results • Inserts, updates, deletes, and moves

Page 58: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Fetch Result Change Details

Fetch results are implicitly registered for change details • Diffs calculated in the background

• Opt-out via fetch options

Get updated fetch result from the PHFetchResultChangeDetails

Page 59: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Handling Example, Part One

- (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]; ...

Page 60: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Handling Example, Part One

- (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]; ...

Page 61: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Handling Example, Part One

- (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]; ...

Page 62: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Handling Example, Part One

- (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]; ...

Page 63: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Handling Example, Part One

- (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]; ...

Page 64: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Handling Example, Part Two

[collectionView performBatchUpdates:^{ if ([[changeDetails removedIndexes] count]) { NSArray *removedIndexPaths = // make indexPaths from indexes [collectionView deleteItemsAtIndexPaths:removedIndexPaths]; } if ([[changeDetails insertedIndexes] count]) { NSArray *insertedIndexPaths = ... [collectionView insertItemsAtIndexPaths:insertedIndexPaths]; } if ([[changeDetails changedIndexes] count]) { NSArray *changedIndexPaths = ... [collectionView reloadItemsAtIndexPaths:changedIndexPaths]; }

Page 65: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Handling Example, Part Two

[collectionView performBatchUpdates:^{ if ([[changeDetails removedIndexes] count]) { NSArray *removedIndexPaths = // make indexPaths from indexes [collectionView deleteItemsAtIndexPaths:removedIndexPaths]; } if ([[changeDetails insertedIndexes] count]) { NSArray *insertedIndexPaths = ... [collectionView insertItemsAtIndexPaths:insertedIndexPaths]; } if ([[changeDetails changedIndexes] count]) { NSArray *changedIndexPaths = ... [collectionView reloadItemsAtIndexPaths:changedIndexPaths]; }

Page 66: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Handling Example, Part Two

[collectionView performBatchUpdates:^{ if ([[changeDetails removedIndexes] count]) { NSArray *removedIndexPaths = // make indexPaths from indexes [collectionView deleteItemsAtIndexPaths:removedIndexPaths]; } if ([[changeDetails insertedIndexes] count]) { NSArray *insertedIndexPaths = ... [collectionView insertItemsAtIndexPaths:insertedIndexPaths]; } if ([[changeDetails changedIndexes] count]) { NSArray *changedIndexPaths = ... [collectionView reloadItemsAtIndexPaths:changedIndexPaths]; }

Page 67: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Change Handling Example, Part Two

[collectionView performBatchUpdates:^{ if ([[changeDetails removedIndexes] count]) { NSArray *removedIndexPaths = // make indexPaths from indexes [collectionView deleteItemsAtIndexPaths:removedIndexPaths]; } if ([[changeDetails insertedIndexes] count]) { NSArray *insertedIndexPaths = ... [collectionView insertItemsAtIndexPaths:insertedIndexPaths]; } if ([[changeDetails changedIndexes] count]) { NSArray *changedIndexPaths = ... [collectionView reloadItemsAtIndexPaths:changedIndexPaths]; }

Page 68: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Demo

Page 69: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Image and Video Data

Karl Hsu iOS Photos Frameworks

Page 70: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Requesting Image and Video Data

A variety of image sizes might be available • Some sizes might not be cached

• Videos might even be streaming

Page 71: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

PHImageManager

Request images based on target size

Request videos based on usage

Asynchronous API

Optionally retrieve data from the network

Page 72: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Requesting Images

// Request image data for a square grid with cells of 160x160 pixels [manager requestImageForAsset:photo targetSize:CGSizeMake(160,160) contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info) { if (result) { [cell setImage:result]; } else { ... } }];

Page 73: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Advanced Image Requests

// Create Image Request object PHImageRequestOptions *options = [PHImageRequestOptions new]; !

// Fetch the image from iCloud if necessary and provide progress options.networkAccessAllowed = YES; options.progressHandler = ^(BOOL degraded, double progress, NSError *error, BOOL *stop) { [self updateUserVisibleProgress:progress error:error]; }; !

// Use the options to control the request behavior [manager requestImageForAsset:... options:options ...];

Page 74: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Advanced Image Requests

// Create Image Request object PHImageRequestOptions *options = [PHImageRequestOptions new]; !

// Fetch the image from iCloud if necessary and provide progress options.networkAccessAllowed = YES; options.progressHandler = ^(BOOL degraded, double progress, NSError *error, BOOL *stop) { [self updateUserVisibleProgress:progress error:error]; }; !

// Use the options to control the request behavior [manager requestImageForAsset:... options:options ...];

Page 75: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Advanced Image Requests

// Create Image Request object PHImageRequestOptions *options = [PHImageRequestOptions new]; !

// Fetch the image from iCloud if necessary and provide progress options.networkAccessAllowed = YES; options.progressHandler = ^(BOOL degraded, double progress, NSError *error, BOOL *stop) { [self updateUserVisibleProgress:progress error:error]; }; !

// Use the options to control the request behavior [manager requestImageForAsset:... options:options ...];

Page 76: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Advanced Image Requests

// Create Image Request object PHImageRequestOptions *options = [PHImageRequestOptions new]; !

// Fetch the image from iCloud if necessary and provide progress options.networkAccessAllowed = YES; options.progressHandler = ^(BOOL degraded, double progress, NSError *error, BOOL *stop) { [self updateUserVisibleProgress:progress error:error]; }; !

// Use the options to control the request behavior [manager requestImageForAsset:... options:options ...];

Page 77: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Image Request Callbacks

[manager requestImageForAsset: ... ^(UIImage *result, NSDictionary *info) { // This block can be called multiple times }];

Page 78: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

[manager requestImageForAsset: ... ^(UIImage *result, NSDictionary *info) { // This block can be called multiple times }];

Image Request Callbacks

First callback synchronous

Page 79: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Image Request Callbacks

[manager requestImageForAsset: ... ^(UIImage *result, NSDictionary *info) { // This block can be called multiple times }];

First callback synchronous

Page 80: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

[manager requestImageForAsset: ... ^(UIImage *result, NSDictionary *info) { // This block can be called multiple times }];

Image Request Callbacks

First callback synchronous Second callback asynchronous

Page 81: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Requesting Videos

// Request a playback item for video playback [manager requestPlayerItemForVideo:video options:nil resultHandler:^(AVPlayerItem *item, NSDictionary *info) { AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem]; }]; !

Page 82: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

// Create Video Request object PHVideoRequestOptions *options = [PHVideoRequestOptions new]; !

// Make sure we have the best quality options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat; !

// Fetch the video from iCloud if necessary and provide progress options.networkAccessAllowed = YES; options.progressHandler = ^(double progress, NSError *error, BOOL *stop) { [self updateUserVisibleProgress:progress error:error]; }; !

// Use the options to control the request behavior [manager requestExportSessionForVideo:video options:options ...];

Advanced Video Requests

Page 83: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

// Create Video Request object PHVideoRequestOptions *options = [PHVideoRequestOptions new]; !

// Make sure we have the best quality options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat; !

// Fetch the video from iCloud if necessary and provide progress options.networkAccessAllowed = YES; options.progressHandler = ^(double progress, NSError *error, BOOL *stop) { [self updateUserVisibleProgress:progress error:error]; }; !

// Use the options to control the request behavior [manager requestExportSessionForVideo:video options:options ...];

Advanced Video Requests

Page 84: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

// Create Video Request object PHVideoRequestOptions *options = [PHVideoRequestOptions new]; !

// Make sure we have the best quality options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat; !

// Fetch the video from iCloud if necessary and provide progress options.networkAccessAllowed = YES; options.progressHandler = ^(double progress, NSError *error, BOOL *stop) { [self updateUserVisibleProgress:progress error:error]; }; !

// Use the options to control the request behavior [manager requestExportSessionForVideo:video options:options ...];

Advanced Video Requests

Page 85: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

// Create Video Request object PHVideoRequestOptions *options = [PHVideoRequestOptions new]; !

// Make sure we have the best quality options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat; !

// Fetch the video from iCloud if necessary and provide progress options.networkAccessAllowed = YES; options.progressHandler = ^(double progress, NSError *error, BOOL *stop) { [self updateUserVisibleProgress:progress error:error]; }; !

// Use the options to control the request behavior [manager requestExportSessionForVideo:video options:options ...];

Advanced Video Requests

Page 86: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

// Create Video Request object PHVideoRequestOptions *options = [PHVideoRequestOptions new]; !

// Make sure we have the best quality options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat; !

// Fetch the video from iCloud if necessary and provide progress options.networkAccessAllowed = YES; options.progressHandler = ^(double progress, NSError *error, BOOL *stop) { [self updateUserVisibleProgress:progress error:error]; }; !

// Use the options to control the request behavior [manager requestExportSessionForVideo:video options:options ...];

Advanced Video Requests

Page 87: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Scrolling Performance

Scrolling a grid of thumbnails

Maintain a cache around the visible range • Start caching ahead of the scroll

• Stop caching behind the scroll

Page 88: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

PHCachingImageManager

Preloads and caches images

Request against the caching image manager • Requests are resolved against cached data

Instance for each view controller

Page 89: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Preheating

Page 90: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Preheating

Page 91: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Preheating

Visible Range

Page 92: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Preheating

Start Caching

Visible Range

Page 93: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Preheating

Start Caching

Stop Caching

Visible Range

Page 94: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Preheating

Start Caching

Stop Caching

Visible Range

Page 95: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

PHCachingImageManager

// Use the same args as for requestImageForAsset PHCachingImageManager *cim = [self cachingImageManager]; !

!

NSArray *soonToBeVisibleAssets = ... [cim startCachingImagesForAssets:soonToBeVisibleAssets targetSize:targetSize contentMode:PHImageContentModeAspectFill options:nil]; !

NSArray *previouslyVisibleAssets = ... [cim stopCachingImagesForAssets:previouslyVisibleAssets targetSize:targetSize contentMode:PHImageContentModeAspectFill options:nil];

Page 96: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

PHCachingImageManager

// Use the same args as for requestImageForAsset PHCachingImageManager *cim = [self cachingImageManager]; !

!

NSArray *soonToBeVisibleAssets = ... [cim startCachingImagesForAssets:soonToBeVisibleAssets targetSize:targetSize contentMode:PHImageContentModeAspectFill options:nil]; !

NSArray *previouslyVisibleAssets = ... [cim stopCachingImagesForAssets:previouslyVisibleAssets targetSize:targetSize contentMode:PHImageContentModeAspectFill options:nil];

Page 97: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

PHCachingImageManager

// Use the same args as for requestImageForAsset PHCachingImageManager *cim = [self cachingImageManager]; !

!

NSArray *soonToBeVisibleAssets = ... [cim startCachingImagesForAssets:soonToBeVisibleAssets targetSize:targetSize contentMode:PHImageContentModeAspectFill options:nil]; !

NSArray *previouslyVisibleAssets = ... [cim stopCachingImagesForAssets:previouslyVisibleAssets targetSize:targetSize contentMode:PHImageContentModeAspectFill options:nil];

Page 98: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Editing Images and Videos

In-place • No need to save as a new asset

Nondestructive

Visible everywhere

Synced via iCloud Photo Library

Page 99: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Editing Flow

Input Image

Page 100: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Editing Flow

Input Image

Page 101: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Editing Flow

Input Image30820bd806092a864886f70d010702a0820bc930820bc5020101310b300906052b0e03021a05003082013c06092a864886f70d010701a082012d048201297b226163746976697479636f6e74696e7561

Your Code

Page 102: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Your Code

Editing Flow

Input Image

Page 103: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Your Code

Editing Flow

Output Image

Page 104: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Getting Input

// Get the input from the asset [asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *editingInput, NSDictionary *info) { !

!

!

!

!

!

!

}

Page 105: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Getting Input

// Get the input from the asset [asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *editingInput, NSDictionary *info) { NSURL *url = [editingInput fullSizeImageURL]; int orientation = [editingInput fullSizeImageOrientation]; !

CIImage *inputImage = [CIImage imageWithContentsOfURL:url options:nil]; inputImage = [inputImage imageByApplyingOrientation:orientation]; !

// Your code here }

Page 106: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Saving Output

// Create the output PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:input]; [jpegData writeToURL:output.renderedContentURL atomically:YES]; output.adjustmentData = adjustmentData; !

Page 107: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Saving Output

// Create the output PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:input]; [jpegData writeToURL:output.renderedContentURL atomically:YES]; output.adjustmentData = adjustmentData; !

// Save the output to the asset [library performChanges:^{ PHAssetChangeRequest *request = ... [request setContentEditingOutput:contentEditingOutput]; } completionHandler:^(BOOL success, NSError *error) {}];

Page 108: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Resumable Edits

Page 109: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Resumable Edits

Noir, 100%+ =Adjustment Data

Page 110: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Resumable Edits

Input

Noir, 100%+ =Adjustment Data

Page 111: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Resumable Edits

Output

Input

Noir, 100%+ =Adjustment Data

Page 112: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Saving Adjustment Data

NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:settings]; !

PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"com.mycompany" formatVersion:@"1.0" data:archivedData]; !

PHContentEditingOutput *output = ... output.adjustmentData = adjustmentData;

Page 113: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Adjustment Data

PHContentEditingInputRequestOptions *options = … !

// Do you understand the current adjustment options.canHandleAdjustmentData:^BOOL(PHAdjustmentData *adjustmentData) { !

}

Page 114: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Adjustment Data

PHContentEditingInputRequestOptions *options = … !

// Do you understand the current adjustment options.canHandleAdjustmentData:^BOOL(PHAdjustmentData *adjustmentData) { return [adjustmentData.formatIdentifier isEqual:@"com.mycompany"] && [adjustmentData.formatVersion isEqual:@"1.0"]; }

Page 115: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Adjustment Understood? Yes!

Input

Noir, 100%+ =Adjustment Data

Page 116: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Adjustment Understood? No.

Input

Noir, 100%+ =Adjustment Data

Page 117: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Demo

Page 118: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Photo Editing ExtensionsPhotosUI framework

Simon Bovet iOS Photos Frameworks

Page 119: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Photo Editing Extension

Your image or video editor

Available from built-in Photos and Camera apps

Page 120: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and
Page 121: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and
Page 122: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and
Page 123: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and
Page 124: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

What’s Needed

App extension target

UIViewController subclass

Protocol adoption

Page 125: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

What’s Needed

App extension target

UIViewController subclass

Protocol adoption

Page 126: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Xcode Template

Add new target to your app’s project

Choose “Photo Editing Extension”

Page 127: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

User Interface

Navigation bar displayed by Photos app

Avoid navigation bar-based design for your extension

Your extension’s UI

Page 128: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

What’s Needed

App extension target

UIViewController subclass

Protocol adoption

Page 129: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Protocol AdoptionPHContentEditingController

startContentEditingWithInput:

finishContentEditingWithCompletionHandler:

canHandleAdjustmentData:

cancelContentEditing

Page 130: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Protocol AdoptionPHContentEditingController

startContentEditingWithInput:

finishContentEditingWithCompletionHandler:

canHandleAdjustmentData:

cancelContentEditing

Page 131: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Protocol AdoptionPHContentEditingController

startContentEditingWithInput:

finishContentEditingWithCompletionHandler:

canHandleAdjustmentData:

cancelContentEditing

Page 132: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Getting Input

- (void)startContentEditingWithInput:(PHContentEditingInput *)input placeholderImage:(UIImage *)placeholderImage { UIImage *image = input.displaySizeImage; !

id settings = [self settingsFromAdjustmentData:input.adjustmentData]; if (!settings) { settings = [self defaultSettings]; } !

... // set up user interface self.input = input; }

Page 133: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Getting Input

- (void)startContentEditingWithInput:(PHContentEditingInput *)input placeholderImage:(UIImage *)placeholderImage { UIImage *image = input.displaySizeImage; !

id settings = [self settingsFromAdjustmentData:input.adjustmentData]; if (!settings) { settings = [self defaultSettings]; } !

... // set up user interface self.input = input; }

Page 134: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Getting Input

- (void)startContentEditingWithInput:(PHContentEditingInput *)input placeholderImage:(UIImage *)placeholderImage { UIImage *image = input.displaySizeImage; !

id settings = [self settingsFromAdjustmentData:input.adjustmentData]; if (!settings) { settings = [self defaultSettings]; } !

... // set up user interface self.input = input; }

Page 135: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Protocol AdoptionPHContentEditingController

startContentEditingWithInput:

finishContentEditingWithCompletionHandler:

canHandleAdjustmentData:

cancelContentEditing

Your extension’s UI

Page 136: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Protocol AdoptionPHContentEditingController

startContentEditingWithInput:

finishContentEditingWithCompletionHandler:

canHandleAdjustmentData:

cancelContentEditing

Your extension’s UI

Page 137: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Saving Output

- (void)finishContentEditingWithCompletionHandler: (void (^)(PHContentEditingOutput *))completionHandler { NSData *jpegData = ...; PHAdjustmentData *adjustmentData = ...; !

PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input]; [jpegData writeToURL:output.renderedContentURL atomically:YES]; output.adjustmentData = adjustmentData; !

completionHandler(output); }

Page 138: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Saving Output

- (void)finishContentEditingWithCompletionHandler: (void (^)(PHContentEditingOutput *))completionHandler { NSData *jpegData = ...; PHAdjustmentData *adjustmentData = ...; !

PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input]; [jpegData writeToURL:output.renderedContentURL atomically:YES]; output.adjustmentData = adjustmentData; !

completionHandler(output); }

Page 139: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Saving Output

- (void)finishContentEditingWithCompletionHandler: (void (^)(PHContentEditingOutput *))completionHandler { NSData *jpegData = ...; PHAdjustmentData *adjustmentData = ...; !

PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input]; [jpegData writeToURL:output.renderedContentURL atomically:YES]; output.adjustmentData = adjustmentData; !

completionHandler(output); }

Page 140: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Protocol AdoptionPHContentEditingController

startContentEditingWithInput:

finishContentEditingWithCompletionHandler:

canHandleAdjustmentData:

cancelContentEditing

Page 141: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Resuming Edits

- (BOOL)canHandleAdjustmentData:(PHAdjustmentData *)adjustmentData { return [adjustmentData.formatIdentifier isEqual:@"com.mycompany"] && [adjustmentData.formatVersion isEqual:@"1.0"]; }

Page 142: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Protocol AdoptionPHContentEditingController

startContentEditingWithInput:

finishContentEditingWithCompletionHandler:

canHandleAdjustmentData:

cancelContentEditing

Your extension’s UI

Page 143: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Protocol AdoptionPHContentEditingController

startContentEditingWithInput:

finishContentEditingWithCompletionHandler:

canHandleAdjustmentData:

cancelContentEditing

Your extension’s UI

Page 144: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

What’s Needed

App extension target

UIViewController subclass

Protocol adoption

Page 145: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Demo

Page 146: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Summary

Photos framework • Access photos and videos

• Build a full-featured app like the Photos app

Photo editing extensions

Page 147: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

More Information

Allan Schaffer Graphics and Game Technologies Evangelist [email protected]

!

Documentation Photos Reference http://developer.apple.com

Apple Developer Forums http://devforums.apple.com

Page 148: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Related Sessions

• Creating Extensions for iOS and OS X, Part One Mission Tuesday 2:00PM

• Creating Extensions for iOS and OS X, Part Two Mission Wednesday 11:30AM

• Camera Capture: Manual Controls Marina Wednesday 11:30AM

• Advances in Core Image Pacific Heights Thursday 2:00PM

• Developing Core Image Filters for iOS Pacific Heights Thursday 3:15PM

Page 149: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and

Labs

• Photos Framework Lab Media Lab A Thursday 11:30AM

• Extensions Lab Frameworks Lab B Thursday 2:00PM

• Photos Framework Lab Media Lab B Friday 10:15AM

Page 150: Introducing the Photos Frameworks - Apple Inc. · What You Will Learn Photos framework • Fetch and manipulate photo library model data • Handle external changes • Retrieve and