---
title: "Photos bites - Little Bites of Cocoa"
description: "Little Bites of Cocoa about Photos: short tips and techniques for iOS and Mac development, each a brief explanation of one concept, technique or tool."
url: "https://littlebitesofcocoa.com/topics/24-photos"
author: "Jake Marsh"
---
# Bites about: [Photos](https://littlebitesofcocoa.com/topics/24-photos)

Topics

- [Extensions](https://littlebitesofcocoa.com/topics/17-extensions)
- [Photos](https://littlebitesofcocoa.com/topics/24-photos)

## [#58: Photo Editing Extensions 🎑📝](https://littlebitesofcocoa.com/58-photo-editing-extensions)

 Wednesday August 12th, 2015

Topics

- [Extensions](https://littlebitesofcocoa.com/topics/17-extensions)
- [Photos](https://littlebitesofcocoa.com/topics/24-photos)

**Photo Editing Extensions** are a powerful way for apps to integrate with the system Photos app. Users can begin editing a photo, jump into a third-party extension, and return seamlessly back to the system editing interface. Let's try making one.

We begin by adding a new **Photo Editing Extension** target to our project.

![Image 1 of 2 from bite #58: Photo Editing Extensions](https://media.littlebitesofcocoa.com/bite-assets/058-photo-editing-extensions-photo-editing-extension-target.png)

Our extension will use **CoreImage** (covered in [Bite #32](https://littlebitesofcocoa.com/32-core-image-basics)) to convert a photo to black and white. The basic workflow goes like this:

1. User launches our extension while editing inside **Photos.app**
2. a `PHContentEditingInput` object is handed to us via the startContentEditingWithInput... function
3. User performs the edits they would like using our view controller
4. User taps **Done** button (provided by the system)
5. System asks us for a `PHContentEditingOutput` object containing our changes to the image via `finishContentEditingWithCompletionHandler`.

![Image 2 of 2 from bite #58: Photo Editing Extensions](https://media.littlebitesofcocoa.com/bite-assets/058-photo-editing-extensions-workflow.png)

Let's move on to the code.

Along with the input object, we also receive a `placeholderImage` which we'll use as the initial image in our own image view.

```swift
func startContentEditingWithInput(contentEditingInput: PHContentEditingInput?, placeholderImage: UIImage) {
  input = contentEditingInput
  imageView.image = placeholderImage
}
```

After processing, we create an output using the intial input object we were handed, and write our modified image to it's content URL.

```swift
let output = PHContentEditingOutput(contentEditingInput: input)

processedImageData.writeToURL(
  output.renderedContentURL,
  atomically: true
)
```

Lastly, we describe our adjustments, so we can be a good citizen in **Photos.app's** non-destructive editing world.

```swift
output.adjustmentData = PHAdjustmentData(
  formatIdentifier: NSBundle.mainBundle().bundleIdentifier!,
  formatVersion: "1.0",
  data: "Grayscale".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
)
```

Download the complete project here: [j.mp/bite058](http://j.mp/bite058)

Topics

- [Photos](https://littlebitesofcocoa.com/topics/24-photos)

## [#33: Photos Framework Basics 🗻](https://littlebitesofcocoa.com/33-photos-framework-basics)

 Wednesday July 8th, 2015

Topics

- [Photos](https://littlebitesofcocoa.com/topics/24-photos)

The **Photos Framework** is the modern equivalent to the old **Assets Library Framework**. It's how you access and interact with the user's photo library on **iOS** and **OS X**. Here are a few examples of things you can do with it:

## Retrieve All Albums

```swift
let fetchOptions = PHFetchOptions()

let albums = PHAssetCollection.fetchAssetCollectionsWithType(
  .Album,
  subtype: .Any,
  options: fetchOptions
)
```

## Mark a Photo As A Favorite

```swift
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
  let request = PHAssetChangeRequest(forAsset: photo)
  request.favorite = true
}, completionHandler: { (successful, error) in })
```

Changes must happen asynchronously when working with the Photos Framework so we use a PHAssetChangeRequest to mark a photo as a favorite.

## Retrieve an Image at Specific Size

```swift
let manager = PHCachingImageManager()

manager.requestImageForAsset(asset,
  targetSize: CGSizeMake(50.0, 50.0),
  contentMode: PHImageContentMode.AspectFill,
  options: nil,
  resultHandler: {
    (image: UIImage?, userInfo: [NSObject : AnyObject]?) in
    // yay image!
  }
)
```

We didn't specify any options there, but you can use the `PHImageRequestOptions` to customize if you'd like to allow loading images over the network, as well as get a progress callback as they are downloaded.

 

 
