我正在制作一个简单的程序,为图像添加过滤器并遇到一些问题。问题是什么
以下是我写的最终代码
//
// 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) {
}
}
只使用一个过滤器时效果很好。但是当我想向用户显示一个列表然后希望他选择他喜欢的过滤器时,它会崩溃。请帮帮我,我该怎么办?
答案 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();
}
}
调用此方法以获取选择器中每个选项的标题。为要显示的选择器视图中的每个项调用它。您似乎正在尝试同时将过滤器应用于图像。我不认为这是你想做的事。