<!--
{
  "documentType" : "article",
  "framework" : "AVFoundation",
  "identifier" : "/documentation/AVFoundation/extracting-portrait-effects-matte-image-data-from-a-photo",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Extracting Portrait Effects matte image data from a photo"
}
-->

# Extracting Portrait Effects matte image data from a photo

Check for portrait effects matte metadata in existing images.

## Discussion

The portrait effects matte is stored in the image file alongside the depth data auxiliary image. You can load, view, and edit the portrait effects matte as a high-level object called an [`AVPortraitEffectsMatte`](/documentation/AVFoundation/AVPortraitEffectsMatte), analogous to [`AVDepthData`](/documentation/AVFoundation/AVDepthData). This mirrors traditional depth map access using [`AVDepthData`](/documentation/AVFoundation/AVDepthData) in its technique to get the elementary auxiliary image bits out of the file.

![Juxtaposition of the depth map and portrait effects matte of a photo showing a girl holding a flower.](images/com.apple.avfoundation/media-3030225@2x.png)

### Load and view a matting image from file

Load the portrait effects matte by using <doc://com.apple.documentation/documentation/ImageIO> to extract an auxiliary image of type <doc://com.apple.documentation/documentation/ImageIO/kCGImageAuxiliaryDataTypePortraitEffectsMatte>. Convert this auxiliary image to an auxiliary information dictionary, which contains the matte as metadata of class <doc://com.apple.documentation/documentation/ImageIO/CGImageMetadata>. Generate the portrait effects matte object, [`AVPortraitEffectsMatte`](/documentation/AVFoundation/AVPortraitEffectsMatte), by passing the dictionary to [`init(fromDictionaryRepresentation:)`](/documentation/AVFoundation/AVPortraitEffectsMatte/init(fromDictionaryRepresentation:)). From this object, you can generate a <doc://com.apple.documentation/documentation/CoreImage/CIImage> object to bring the image into viewable forms, like <doc://com.apple.documentation/documentation/UIKit/UIImage>.

```swift
func portraitEffectsMatteImage(at path: String) -> UIImage? {
    let bundlePath = Bundle.main.bundlePath
    let fileURL = URL(fileURLWithPath: bundlePath).appendingPathComponent(path)
        
    guard let source = CGImageSourceCreateWithURL(fileURL as CFURL, nil),
          let auxiliaryInfoDict = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypePortraitEffectsMatte) as? [AnyHashable: Any] else { return nil }
        
    // Create a portrait effects matte from the auxiliary information.
    if let matteData = try? AVPortraitEffectsMatte(fromDictionaryRepresentation: auxiliaryInfoDict),
       let matteCIImage = CIImage(portaitEffectsMatte: matteData) {
        // Return a matte image by using the core image representation.
        return UIImage(ciImage: matteCIImage)
    }
    return nil
}
```

With this matte image, your app can:

- Access the uncompressed pixels of the portrait effects matte in memory.
- Create rotated or flipped derivative copies of the mask.
- Replace a pixel of the matte with one of your own, reflecting an effect you’re applying to the main image.
- Create a dictionary of elementary PEM parts suitable for writing to a file using Image I/O’s <doc://com.apple.documentation/documentation/ImageIO/CGImageDestination>.

When decompressed, the matte images are natively L008 (<doc://com.apple.documentation/documentation/CoreVideo/kCVPixelFormatType_OneComponent8>), which is an 8-bit one-component format where black is zero. Portrait effects matte images are not gamma corrected. They’re tagged with a linear transfer function, indicating that no color correction should be applied when working with them.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)