来自 CIFilter 的图像不会显示
Image from CIFilter wont display
我正在拍摄子视图的快照并将其转换为图像:
let image = UIImage(view: swiftyDraw)
let scaledImage = scaleImage(image: image, toSize: CGSize(width: 28, height:28))
finalImage 是 UIImageView
并将其设置为显示 scaledImage 有效,我可以很好地看到它:
finalImage.image = scaledImage //this works
但是,我想在显示之前反转颜色:
let filter = CIFilter(name: "CIColorInvert")
filter!.setValue(CIImage(image: scaledImage), forKey: kCIInputImageKey)
let invertedImage = UIImage(ciImage: filter!.outputImage!)
finalImage.image = invertedImage //this does not work
当我将 finalImage.image 设置为 invertedImage 时没有任何显示,但是当它设置为 scaledImage 时它显示正常。
编辑:scaleImage 函数:
func scaleImage (image: UIImage, toSize size:CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 1.0)
image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
假设你的 "scaledImage" 是一个 UIImage
,那么你可以直接得到一个 CIImage
而无需创建一个新的。
guard let ciImage = scaledImage.ciImage, let filter = CIFilter(name: "CIColorInvert") else {
fatalError("CIImage conversion fail")
}
//Now access the CIFilter object to filter the image
filter.setValue(ciImage, forKey: kCIInputImageKey)
finalImage.image = UIImage(ciImage: filter.outputImage!)
Update
以防万一,如果你是 fatalError
意味着 UIImage
不存在 CIImage
,所以你需要将 UIImage
转换为 CIImage
。
extension UIImage {
var toCIImage: CIImage {
return self.CIImage ?? CIImage(CGImage: self.CGImage!)
}
}
guard let filter = CIFilter(name: "CIColorInvert") else {
fatalError("CIImage conversion fail")
}
let ciImage = scaledImage.toCIImage
//Now access the CIFilter object to filter the image
filter.setValue(ciImage, forKey: kCIInputImageKey)
finalImage.image = UIImage(ciImage: filter.outputImage!)
我正在拍摄子视图的快照并将其转换为图像:
let image = UIImage(view: swiftyDraw)
let scaledImage = scaleImage(image: image, toSize: CGSize(width: 28, height:28))
finalImage 是 UIImageView
并将其设置为显示 scaledImage 有效,我可以很好地看到它:
finalImage.image = scaledImage //this works
但是,我想在显示之前反转颜色:
let filter = CIFilter(name: "CIColorInvert")
filter!.setValue(CIImage(image: scaledImage), forKey: kCIInputImageKey)
let invertedImage = UIImage(ciImage: filter!.outputImage!)
finalImage.image = invertedImage //this does not work
当我将 finalImage.image 设置为 invertedImage 时没有任何显示,但是当它设置为 scaledImage 时它显示正常。
编辑:scaleImage 函数:
func scaleImage (image: UIImage, toSize size:CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 1.0)
image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
假设你的 "scaledImage" 是一个 UIImage
,那么你可以直接得到一个 CIImage
而无需创建一个新的。
guard let ciImage = scaledImage.ciImage, let filter = CIFilter(name: "CIColorInvert") else {
fatalError("CIImage conversion fail")
}
//Now access the CIFilter object to filter the image
filter.setValue(ciImage, forKey: kCIInputImageKey)
finalImage.image = UIImage(ciImage: filter.outputImage!)
Update
以防万一,如果你是 fatalError
意味着 UIImage
不存在 CIImage
,所以你需要将 UIImage
转换为 CIImage
。
extension UIImage {
var toCIImage: CIImage {
return self.CIImage ?? CIImage(CGImage: self.CGImage!)
}
}
guard let filter = CIFilter(name: "CIColorInvert") else {
fatalError("CIImage conversion fail")
}
let ciImage = scaledImage.toCIImage
//Now access the CIFilter object to filter the image
filter.setValue(ciImage, forKey: kCIInputImageKey)
finalImage.image = UIImage(ciImage: filter.outputImage!)