Adding Filter To Images Swift

Haris Sandhu

I was making a simple program that adds filters to images and ran into some problems there. What the problem are

  1. It is very Slow.
  2. When scrolling through filters in the UI, it throws exception.

fatal error: unexpectedly found nil while unwrapping an Optional value

See the Image for description

And below is the final code i have written

//
//  ViewController.swift
//  ImageManipulationWithSwift
//
//  Created by Salman Ali on 01/03/2017.
//  Copyright © 2017 Antzion. All rights reserved.
//

import UIKit
import CoreImage


class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

var pickerFilters: [String] = [String]()
let path = Bundle.main.path(forResource: "scientist", ofType: "jpg")!

@IBOutlet weak var filterPicker: UIPickerView!
@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {

    super.viewDidLoad()
    let categFilters = CIFilter.filterNames(inCategory: kCICategoryColorEffect)

    //pickerFilters.append("CISepiaTone")
    for f in categFilters{
        pickerFilters.append(f)
    }
    filterPicker.dataSource = self
    filterPicker.delegate = self
    applyFilter(index: 0)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func applyFilter(index: Int){
    let context = CIContext()
    // 1
    //below two lines will refer the code to GPU instead of cpu
    //let openGLContext = EAGLContext(api: .openGLES2)
    //let context = CIContext(eaglContext: openGLContext!)
    let filter = CIFilter(name: pickerFilters[index])!
    // 2

    //filter.setValue(0.8, forKey: kCIInputIntensityKey)
    let image = CIImage(contentsOf: URL(fileURLWithPath: path))                          // 3
    filter.setValue(image, forKey: kCIInputImageKey)
    let result = filter.outputImage!                                    // 4
    let cgImage = context.createCGImage(result, from: result.extent)    // 5
    //load this image to imageview
    imageView.image = UIImage(cgImage: cgImage!)
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerFilters.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    self.applyFilter(index: row)
    return pickerFilters[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

}


}

It works fine when only one filter is used. But when i want to show a list to the user and then want him to choose the filter he likes, it crashes. Please help me what should i do?

Abizern

The problem is with your delegate implementation:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    self.applyFilter(index: row)
    return pickerFilters[row]
}

this is called to get the title to display for each option in the picker. It is called for each item in the picker view that is to be displayed. You seem to be trying to apply the filter to the image at the same time. I don't think this is what you want to do.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related