Swift 图片滤镜

Swift Image Filter

照片上的过滤有效,但我遇到了这个问题。


当我运行这段代码时:

let originalImage = CIImage(image: imageView.image)

var filter = CIFilter(name: "CIPhotoEffectMono")

filter.setDefaults()
filter.setValue(originalImage, forKey: kCIInputImageKey)

var newImage = UIImage(CIImage: filter.outputImage)
imageView.image = newImage

第一个错误是:

BSXPCMessage received error for message: Connection interrupted


第二个错误是:

fatal error: unexpectedly found nil while unwrapping an Optional value


然后应用程序崩溃。

我该如何修复它和错误?

试试这个:

let originalImage = CIImage(image: imageView.image)
var filter = CIFilter(name: "CIPhotoEffectMono")
 filter.setValue(originalImage, forKey: kCIInputImageKey)
let context = CIContext(options:nil)
let ouputImage = context.createdCGImage(filter,fromRect:filter.extend())
 var newImage = UIImage(CGImage: ouputImage)
   imageView.image = newImage

我找到了解决方案:

已更新

let originalImage = CIImage(image: imageView.image)
var filter = CIFilter(name: "CIPhotoEffectMono")
filter.setValue(originalImage, forKey: kCIInputImageKey)
let context = CIContext(options: [kCIContextUseSoftwareRenderer: true])
let outputImage = context.createCGImage(filter.outputImage, fromRect: filter.outputImage.extent())
var newImage = UIImage(CGImage: outputImage)
imageView.image = newImage

*基于 Lamar 的 代码。


CIContext(options: nil) 导致 BSXPCMessage received error for message: Connection interrupted 错误。

所以替换:

CIContext(options: nil)CIContext(options: [kCIContextUseSoftwareRenderer: true])


感谢您的帮助:

拉马尔

应用程序不再崩溃。

您的代码运行良好。当你调用它时,你的图像源 imageView.image 可能是 nil 。你可以在游乐场试试这个:

let myPicture = UIImage(data: NSData(contentsOfURL: NSURL(string:"http://i.stack.imgur.com/Xs4RX.jpg")!)!)!

let filter = CIFilter(name: "CIPhotoEffectMono")
filter.setValue(CIImage(image: myPicture), forKey: kCIInputImageKey)
let newImage = UIImage(CIImage: filter.outputImage)
**I have added Black n White Effect To an Image** 

UIGraphicsBeginImageContext(clickedImage.bounds.size)
                clickedImage.layer.renderInContext(UIGraphicsGetCurrentContext())
                var image2: UIImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                origionalimage=image2;
                var CIfilterName = "CIPhotoEffectTonal"
                let ciContext = CIContext(options: nil)
                let startImage = CIImage(image: image2)
                let filter = CIFilter(name: CIfilterName)
                filter.setDefaults()
                filter.setValue(startImage, forKey: kCIInputImageKey)
                let filteredImageData = filter.valueForKey(kCIOutputImageKey) as! CIImage
                let filteredImageRef = ciContext.createCGImage(filteredImageData, fromRect: filteredImageData.extent())
                var newImage = UIImage(CGImage: filteredImageRef)!
                self.mediaData = UIImagePNGRepresentation(newImage )
                self.videoThumbData = UIImageJPEGRepresentation(image2, 0.1)
                self.clickedImage.image = newImage;