我正在使用swift 2并遇到一个奇怪的问题,同时按照教程选择带有选择器的图像,然后按下按钮时应用过滤器。
我的流程是这样的:
这是图像选择器的代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
if let pickedImage = image as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
这部分进行处理/过滤:
func setProcessFilter() {
var originalImage = CIImage(image: imageView.image!)
if originalImage !== nil {
var filter = CIFilter(name: "CIPhotoEffectProcess")
filter?.setDefaults()
filter?.setValue(originalImage, forKey: kCIInputImageKey)
var imageOutput = filter!.outputImage
var newImage = UIImage(CIImage: imageOutput!)
imageView.image = newImage
} else {
print("problem!")
}
}
有趣的是,这在第一次点击按钮时起作用,但是第二次原始图像变为nill。我不确定为什么会这样?
答案 0 :(得分:0)
对于在阅读教程时遇到此问题的任何人,只需创建一个var:
var filterImage : CIImage!
并且一旦imagepicker选择了图像,请将其推到那里:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
if let pickedImage = image as? UIImage {
imageView.contentMode = .ScaleAspectFit
filterImage = CIImage(image: pickedImage)
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}