添加过滤器到图像Swift

时间:2017-03-01 15:06:29

标签: ios swift3 xcode8 core-image imagefilter

我正在制作一个简单的程序,为图像添加过滤器并遇到一些问题。问题是什么

  1. 非常慢。
  2. 在UI中滚动浏览器时,会抛出异常。
  3. 致命错误:在解包可选值时意外发现nil

    See the Image for description

    以下是我写的最终代码

    //
    //  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) {
    
    }
    
    
    }
    

    只使用一个过滤器时效果很好。但是当我想向用户显示一个列表然后希望他选择他喜欢的过滤器时,它会崩溃。请帮帮我,我该怎么办?

1 个答案:

答案 0 :(得分:1)

问题在于您的代理实施:

public class GlobalServer{
GlobalServer(){new Thread(() ->{
    try {
        serverSocket = new ServerSocket(1234);
        Socket clientSocket;
        while (true) {
            clientSocket = serverSocket.accept();
            handleClient(clientSocket);
        }
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}).start();
}

public void handleClient(Socket clientSocket) throws IOException, ClassNotFoundException{
    ObjectInputStream is = new ObjectInputStream(clientSocket.getInputStream());
    Object [] objArr = (Object[]) is.readObject();
    msgHandler(objArr, clientSocket);
}

public void msgHandler(Object [] objArr, Socket clientSocket){
    int msg_type = (int) objArr[0];
    switch (msg_type) {
    case 1:
        System.out.println("type 1 received");
        break;
    case 2:
        System.out.println("type 2 received");
        break;
    case 3:
        System.out.println("type 3 received");
        break;

    default:
        break;
    }
}
public static void main(String [] args){
    GlobalServer s = new GlobalServer();
}
}

调用此方法以获取选择器中每个选项的标题。为要显示的选择器视图中的每个项调用它。您似乎正在尝试同时将过滤器应用于图像。我不认为这是你想做的事。