<!--
{
  "documentType" : "article",
  "framework" : "CoreImage",
  "identifier" : "/documentation/CoreImage/processing-an-image-using-built-in-filters",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Processing an Image Using Built-in Filters"
}
-->

# Processing an Image Using Built-in Filters

Apply effects such as sepia tint, highlight strengthening, and scaling to images.

## Discussion

You can add effects to images by applying Core Image filters to [`CIImage`](/documentation/CoreImage/CIImage) objects. Figure 1 shows three filters chained together to achieve a cumulative effect:

1. Apply the sepia filter to tint an image with a reddish-brown hue.
2. Add the bloom filter to accentuate highlights.
3. Use the Lanczos scale filter to scale an image down.

![Photo of a waterwheel filtered using sepia tone, bloom, and Lanczos scale filters](images/com.apple.coreimage/media-2955144.png)

### Create a Context

[`CIImage`](/documentation/CoreImage/CIImage) processing occurs in a [`CIContext`](/documentation/CoreImage/CIContext) object. Creating a [`CIContext`](/documentation/CoreImage/CIContext) is expensive, so create one during your initial setup and reuse it throughout your app.

```swift
CIContext* context = [CIContext context];
```

### Load an Image to Process

The next step is to load an image to process. This example loads an image from the project bundle.

```swift
NSURL* imageURL = [[NSBundle mainBundle] URLForResource:@"YourImageName" withExtension:@"png"];
CIImage* originalCIImage = [CIImage imageWithContentsOfURL:imageURL];
self.imageView.image = [UIImage imageWithCIImage:originalCIImage];
```

The [`CIImage`](/documentation/CoreImage/CIImage) object isn’t itself a displayable image, but rather image data. To display it, you must convert it to another type, such as <doc://com.apple.documentation/documentation/UIKit/UIImage>.

### Apply Built-In Core Image Filters

A [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) represents a single operation or recipe for a particular effect. To process a [`CIImage`](/documentation/CoreImage/CIImage) object, pass it through [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) objects. You can subclass [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) or draw from the existing library of built-in filters.

#### Tint Reddish-Brown with the Sepia Filter

Although you can chain filters without separating them into functions, the following example shows how to configure a single [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class), the [`sepiaTone()`](/documentation/CoreImage/CIFilter-swift.class/sepiaTone()) filter.

```swift
- (CIImage*) sepiaFilterImage: (CIImage*)inputImage withIntensity:(CGFloat)intensity {
    CIFilter<CISepiaTone>* sepiaFilter = CIFilter.sepiaToneFilter;
    sepiaFilter.inputImage = inputImage;
    sepiaFilter.intensity = intensity;
    return sepiaFilter.outputImage;
}
```

To pass the image through the filter, call the sepia filter function.

```swift
CIImage* sepiaCIImage = [self sepiaFilterImage:originalCIImage withIntensity:0.9];
```

You can check the intermediate result at any point in the filter chain by converting from [`CIImage`](/documentation/CoreImage/CIImage) to a <doc://com.apple.documentation/documentation/UIKit/UIImage>. You can then assign this <doc://com.apple.documentation/documentation/UIKit/UIImage> to a <doc://com.apple.documentation/documentation/UIKit/UIImageView> for display.

```swift
_imageView.image = [UIImage imageWithCIImage:sepiaCIImage];
```

#### Strengthen Highlights with the Bloom Filter

The bloom filter accentuates the highlights of an image. You can apply it as part of a chain without factoring it into a separate function, but this example encapsulates its functionality into a function.

```swift
- (CIImage*) bloomFilterImage: (CIImage*)inputImage withIntensity:(CGFloat)intensity radius:(CGFloat)radius {
    CIFilter<CIBloom>* bloomFilter = CIFilter.bloomFilter;
    bloomFilter.inputImage = inputImage;
    bloomFilter.intensity = intensity;
    bloomFilter.radius = radius;
    return bloomFilter.outputImage;
}
```

Like the sepia filter, the intensity of the bloom filter’s effect ranges between 0 and 1, with 1 being the most intense effect. The bloom filter has an additional r`adius` parameter to determine how much the glowing regions expand. Experiment with a range to values to fine tune the effect, or assign the input parameter to a control like a <doc://com.apple.documentation/documentation/UIKit/UISlider> to allow your users to tweak its values.

> Note:
> The ``doc://com.apple.coreimage/documentation/CoreImage/CIFilter-swift.class/gloom()`` filter performs the opposite effect.

To display the output, convert the [`CIImage`](/documentation/CoreImage/CIImage) to a <doc://com.apple.documentation/documentation/UIKit/UIImage>.

```swift
CIImage* bloomCIImage = [self bloomFilterImage:sepiaCIImage withIntensity:1 radius:10];
_filteredImageView.image = [UIImage imageWithCIImage:bloomCIImage];
```

#### Scale Image Size with the Lanczos Scale Filter

Apply the [`lanczosScaleTransform()`](/documentation/CoreImage/CIFilter-swift.class/lanczosScaleTransform()) to obtain a high-quality downsampling of the image, preserving the original image’s aspect ratio through the [`lanczosScaleTransform()`](/documentation/CoreImage/CIFilter-swift.class/lanczosScaleTransform()) filter’s parameter `aspectRatio`. For built-in Core Image filters, calculate the aspect ratio as the image’s width over height.

```swift
CGFloat imageWidth = originalUIImage.size.width;
CGFloat imageHeight = originalUIImage.size.height;
CGFloat aspectRatio = imageHeight / imageWidth;
CIImage* scaledCIImage = [self scaleFilterImage:bloomCIImage withAspectRatio:aspectRatio scale:0.05];
```

Like other built-in filters, the [`lanczosScaleTransform()`](/documentation/CoreImage/CIFilter-swift.class/lanczosScaleTransform()) filter also outputs its result as a [`CIImage`](/documentation/CoreImage/CIImage).

```swift
- (CIImage*) scaleFilterImage: (CIImage*)inputImage withAspectRatio:(CGFloat)aspectRatio scale:(CGFloat)scale {
    CIFilter<CILanczosScaleTransform>* scaleFilter = CIFilter.lanczosScaleTransformFilter;
    scaleFilter.inputImage = inputImage;
    scaleFilter.scale = scale;
    scaleFilter.aspectRatio = aspectRatio;
    return scaleFilter.outputImage;
}
```

> Important:
> To optimize computation, Core Image doesn’t actually render any intermediate ``doc://com.apple.coreimage/documentation/CoreImage/CIImage`` result until you force the ``doc://com.apple.coreimage/documentation/CoreImage/CIImage`` to display its content onscreen, as you might do using <doc://com.apple.documentation/documentation/UIKit/UIImageView>.

```swift
_imageView.image = [UIImage imageWithCIImage:scaledCIImage];
```

> Note:
> Core Image optimizes filtering by reordering the three chained filters and concatenating them into a single image processing kernel, saving computation and rendering cycles.

In addition to trying out the built-in filters for a fixed effect, you can combine filters in certain Filter Recipes to accomplish tasks such as [Applying a Chroma Key Effect](/documentation/CoreImage/applying-a-chroma-key-effect), [Selectively Focusing on an Image](/documentation/CoreImage/selectively-focusing-on-an-image), [Customizing Image Transitions](/documentation/CoreImage/customizing-image-transitions), and [Simulating Scratchy Analog Film](/documentation/CoreImage/simulating-scratchy-analog-film).

---

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)