<commit_before># iOS8 Day-by-Day :: Day 23 :: Photo Extension

This post is part of a daily series of posts introducing the most exciting new
parts of iOS8 for developers - [#iOS8DayByDay](https://twitter.com/search?q=%23iOS8DayByDay).
To see the posts you've missed check out the
[introduction page](http://www.shinobicontrols.com/ios8daybyday),
but have a read through the rest of this post first!

---

## Introduction

iOS8 Day-by-Day has looked at a couple of the new extensions in iO8 already -
the share extension and the today extension. In today's article you're going to
learn the basics behind the photo extension.

Extension points are ways that you can write your own code and functionality
which enhance the OS in more far-reaching ways than just your app. In the case
of photo extensions, you can create filters and more complex editing
functionality that will be available to the Photos app.

![Edit in Progress](assets/edit_in_progress.png)

The sample code which accompanies today's article builds a photo extension from
the chromakey core image filter that was used in day 19. You can get the source
code from the ShinobiControls github at
[github.com/ShinobiControls/iOS8-day-by-day](https://github.com/ShinobiControls/iOS8-day-by-day).


## Creating a Photo Extension

In the same way as for the other extension types, the easiest way to create a
photos extension is to use the template provided by Xcode 6:

![Extension Template](assets/extension_template.png)

This create a new target, and within it a subclass of `UIViewController`, which
adopts the `PHContentEditingController` protocol. This adds some lifecycle
methods which are are specific to photo extensions.

You can probably guess from the fact that this is a view controller, that a
photo extension has UI which is provided by you. In fact, the view controller is
presented with a navigation bar containing the name of the photo extension. The
rest is up to you. Since it's just a subclass of `UIViewController` you can use
all the usual techniques, including creating the design in a storyboard.

In the ChromaKey extension which accompanies this article, a simple design
including a slider is used to display the live result from the filter, and to
configure the threshold. This layout is created in the storyboard.

The project also contains the custom `CIFilter` class from day 19 - and the 
`PhotoEditingViewController` contains a reference to one:

    class PhotoEditingViewController: UIViewController, PHContentEditingController {
      
      let filter = ChromaKeyFilter()
      ...
    }

You'll use this filter both during the interactive editing phase, and also when
rendering the final image.

## Starting Interactive Editing

One of the methods defined on `PHContentEditingController` signifies the
beginning of the photo editing process - 
`startContentEditingWithInput(contentEditingInput:, placeholderImage:)`. This is
called when the user selects your extension from the list in the edit menu:

![Choosing Extension](assets/choosing_extension.png)

At this point here it is your responsibility to grab hold of the image, provide
it to your filter and enable the interactive controls which allow the user to
edit the photo.

    func startContentEditingWithInput(contentEditingInput: PHContentEditingInput?, placeholderImage: UIImage) {
      input = contentEditingInput
      filter.inputImage = CIImage(image: input?.displaySizeImage)
      thresholdSlider.value = filter.threshold
      updateOutputImage()
    }

You are provided a `PHContentEditingInput` object, which includes info about the
media you're editing. Importantly, there are several different ways you can get
hold of an input image:

- The supplied `placeholderImage`. This is a `UIImage` of what the output of
your filter provided last time (if you're using resumable editing). Use this if
it takes a long time to set up your filter process.<commit_msg>Adds a disclaimer about the pointlessness of this sample :)<commit_after># iOS8 Day-by-Day :: Day 23 :: Photo Extension

This post is part of a daily series of posts introducing the most exciting new
parts of iOS8 for developers - [#iOS8DayByDay](https://twitter.com/search?q=%23iOS8DayByDay).
To see the posts you've missed check out the
[introduction page](http://www.shinobicontrols.com/ios8daybyday),
but have a read through the rest of this post first!

---

## Introduction

iOS8 Day-by-Day has looked at a couple of the new extensions in iO8 already -
the share extension and the today extension. In today's article you're going to
learn the basics behind the photo extension.

Extension points are ways that you can write your own code and functionality
which enhance the OS in more far-reaching ways than just your app. In the case
of photo extensions, you can create filters and more complex editing
functionality that will be available to the Photos app.

![Edit in Progress](assets/edit_in_progress.png)

The sample code which accompanies today's article builds a photo extension from
the chromakey core image filter that was used in day 19. You can get the source
code from the ShinobiControls github at
[github.com/ShinobiControls/iOS8-day-by-day](https://github.com/ShinobiControls/iOS8-day-by-day).


## Creating a Photo Extension

In the same way as for the other extension types, the easiest way to create a
photos extension is to use the template provided by Xcode 6:

![Extension Template](assets/extension_template.png)

This create a new target, and within it a subclass of `UIViewController`, which
adopts the `PHContentEditingController` protocol. This adds some lifecycle
methods which are are specific to photo extensions.

You can probably guess from the fact that this is a view controller, that a
photo extension has UI which is provided by you. In fact, the view controller is
presented with a navigation bar containing the name of the photo extension. The
rest is up to you. Since it's just a subclass of `UIViewController` you can use
all the usual techniques, including creating the design in a storyboard.

In the ChromaKey extension which accompanies this article, a simple design
including a slider is used to display the live result from the filter, and to
configure the threshold. This layout is created in the storyboard.

The project also contains the custom `CIFilter` class from day 19 - and the 
`PhotoEditingViewController` contains a reference to one:

    class PhotoEditingViewController: UIViewController, PHContentEditingController {
      
      let filter = ChromaKeyFilter()
      ...
    }

You'll use this filter both during the interactive editing phase, and also when
rendering the final image. Note that this doesn't necessarily represent a great
use-case here: the ChromaKey filter relies on you creating an image which includes
transparency which doesn't fit with rendering as JPEG. The concepts introduced
here are all sound, and it wouldn't be too difficult to develop this extension
into something more useful.

## Starting Interactive Editing

One of the methods defined on `PHContentEditingController` signifies the
beginning of the photo editing process - 
`startContentEditingWithInput(contentEditingInput:, placeholderImage:)`. This is
called when the user selects your extension from the list in the edit menu:

![Choosing Extension](assets/choosing_extension.png)

At this point here it is your responsibility to grab hold of the image, provide
it to your filter and enable the interactive controls which allow the user to
edit the photo.

    func startContentEditingWithInput(contentEditingInput: PHContentEditingInput?, placeholderImage: UIImage) {
      input = contentEditingInput
      filter.inputImage = CIImage(image: input?.displaySizeImage)
      thresholdSlider.value = filter.threshold
      updateOutputImage()
    }

You are provided a `PHContentEditingInput` object, which includes info about the
media you're editing. Importantly, there are several different ways you can get
hold of an input image:

- The supplied `placeholderImage`. This is a `UIImage` of what the output of
your filter provided last time (if you're using resumable editing). Use this if
it takes a long time to set up your filter process.
{'fraction_non_alphanumeric': 0.04913646314765264, 'fraction_numerical': 0.004378496716127463, 'mean_word_length': 4.597685500340368, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4612828', 'n_tokens_mistral': 2109, 'n_tokens_neox': 1999, 'n_words': 1114}