Contents

depthOfField()

Simulates a depth of field effect.

Declaration

class func depthOfField() -> any CIFilter & CIDepthOfField

Return Value

The modified image.

Discussion

This method applies the depth of field filter to an image. The effect simulates changing the focus of the camera before taking a photograph.

The depth of field filter uses the following properties:

inputImage

An image with the type CIImage.

radius

A float representing the area of effect as an NSNumber.

point0

A set of coordinates marking the first point to be focused on as a CGPoint.

point1

A set of coordinates marking the second point to be focused on as a CGPoint.

unsharpMaskRadius

A float representing the radius of the unsharpened mask effect applied to the in-focus area of effect as an NSNumber.

unsharpMaskIntensity

A float representing the intensity of the unsharp mask effect as an NSNumber.

The following code creates a filter that results in the center cilantro being in focus while gradually blurring to the top and bottom of the image:

func depthOfField(inputImage: CIImage) -> CIImage {
    let depthOfFieldFilter = CIFilter.depthOfField()
    depthOfFieldFilter.inputImage = inputImage
    depthOfFieldFilter.radius = 5
    depthOfFieldFilter.point0 = CGPoint(x: 2349, y: 846)
    depthOfFieldFilter.point1 = CGPoint(x: 571, y: 3121)
    depthOfFieldFilter.unsharpMaskRadius = 7
    depthOfFieldFilter.unsharpMaskIntensity = 10
    return depthOfFieldFilter.outputImage!
}

[Image]

See Also

Filters