Home Ask Login Register

Developers Planet

Your answer is one click away!

GMHSJ February 2016

What is happening with this code - IOS/Swift

I am developing a camera application which can detect rectangles in video feed and I found a sample project anyhow. But there is a thing that I still can not understand what is happening in there.

Here is the code

class CameraViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate 
    var applyFilter: ((CIImage) -> CIImage?)?;

    override func viewDidLoad() {


        applyFilter = {
            image in
            return self.performRectangleDetection(image);

    func performRectangleDetection(image: CIImage) -> CIImage? {

        //code returns CIImage

    func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {

        let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        let opaqueBuffer = Unmanaged<CVImageBuffer>.passUnretained(imageBuffer!).toOpaque();
        let pixelBuffer = Unmanaged<CVPixelBuffer>.fromOpaque(opaqueBuffer).takeUnretainedValue();
        let sourceImage = CIImage(CVPixelBuffer: pixelBuffer, options: nil);
        let detectionResult = applyFilter?(sourceImage);

This is not the full code actually.I put necessary parts only. Can someone please explain me what is happening with the variable applyFilter. Is it a closure? if it is a closure what is the purpose of using a closure here. Any help would be highly appreciated.

Edited: I just need to know whether there is a special behaviour other than just using a function call


Gavin February 2016

The variable applyFilter can point to a closure or function which takes a CIImage as a parameter and returns a CIImage. In the viewDidLoad() method, this variable is actually being set to a closure, which simply calls another method, passing it the image that was being passed in, and simply returning the return value of that method. So in this case the closure is completely unnecessary, since it is never set to anything else (unless somewhere in the code you didn't include, it gets changed to something different). In any place that calls the applyFilter closure, it could instead just call performRectangleDetection instead.

Now if keeping applyFilter is still desired, perhaps because you do actually set it to something different somewhere else in the code, then its usage in viewDidLoad() can still be simplified. The signature of applyFilter is the same as that of performRectangleDetection, so instead of the code you have:

applyFilter = {
    image in
    return self.performRectangleDetection(image);

You could instead just replace that with:

applyFilter = performRectangleDetection

What this does is makes the applyFilter variable point to the performRectangleDetection method directly, avoiding the intermediate closure that is unnecessary.

Post Status

Asked in February 2016
Viewed 2,517 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life