Creating the ImageFiltering protocol
You need a way to apply a filter to an image. You will create a protocol, ImageFiltering
, that implements a method, apply(filter:originalImage:)
, to do this. Any class that adopts this protocol will have access to this method, which applies a specified filter to an image. Follow these steps:
- Right-click the
PhotoFilter
folder and select New File. - iOS should already be selected. Choose Swift File and click Next.
- Name this file
ImageFiltering
. Click Create.ImageFiltering.swift
will appear in the Project navigator. - Add the following code to this file to declare and define the
ImageFiltering
protocol:import UIKit import CoreImage protocol ImageFiltering { func apply(filter:String, originalImage:UIImage) -> UIImage } extension ImageFiltering { func apply(filter:String, originalImage:UIImage) -> UIImage { ...