现在正在研究swift,并且无法解决这个Core Image Framework问题。我能够在图像上成功创建CIFilter但是我不确定如何删除它。图像被放置在类似于Snapchat的相机屏幕的UIView上,然后有一个图像视图,它是预览图像的uiview的子视图。 BTW使用最新版本的Xcode和iOS。
以下是应用黑白滤镜时的代码:
@IBAction func BW_Flt_Bt_Tapped(sender: UIButton) {
let beginImage = CIImage(image: imagePreview.image!)
let ciContext = CIContext(options: nil)
let filter = CIFilter(name: "CIPhotoEffectNoir")
var imgOrientation = imagePreview.image!.imageOrientation
var imgScale = imagePreview.image?.scale
filter!.setDefaults()
filter!.setValue(beginImage, forKey: kCIInputImageKey)
let filteredImageData = filter!.valueForKey(kCIOutputImageKey) as! CIImage
let filteredImageRef = ciContext.createCGImage(filteredImageData, fromRect: filteredImageData.extent)
imagePreview.image = UIImage(CGImage: filteredImageRef, scale: 1.0, orientation: UIImageOrientation.Right)
self.orgImgBt.hidden = false
self.orgImgL.hidden = false
BWFlt.hidden = true
BWLbl.hidden = true
}
-imagePreview是UIView的子视图
接下来是关于如何更改图像的代码
@IBAction func orgImgPressed(sender: UIButton) {
self.imagePreview.image = self.selctedImage
orgImgBt.hidden = true
orgImgL.hidden = true
`enter code here` BWFlt.hidden = false
`enter code here`BWLbl.hidden = false
print("button was pressed")
}
如果有人有任何建议我会非常感激,谢谢!
答案 0 :(得分:1)
您可以通过将先前值保留在变量中,或者如果您有多个过滤器将这些值保留在字典中。当您应用新过滤器或更改现有过滤器的值时,只需将该值替换为新值并应用原始CIImage上的所有值
例如,
var allFilters = [String: [String: Any]]()
var currentImage: CIImage!
let kCIColorControls = "CIColorControls"
override func viewDidLoad() {
super.viewDidLoad()
currentImage = CIImage(image: self.originalImage!)
}
func applyAvailableFilters() {
if self.allFilters.count > 0 {
DispatchQueue.global(qos: .background).async {
var outputImage: CIImage!
self.blurFilter = CIFilter.init(name: "CIGaussianBlur")
self.brightnessFilter = CIFilter.init(name: "CIColorControls")
self.cropFilter = CIFilter.init(name: "CICrop")
if let blurProperty = self.allFilters[self.kCIGaussianBlur]?.first {
self.blurFilter.setValue(blurProperty.value, forKey: blurProperty.key)
self.blurFilter.setValue(self.currentImage, forKey: kCIInputImageKey)
outputImage = self.blurFilter.outputImage
}
if let brightnessProperty = self.allFilters[self.kCIColorControls]?.first {
self.brightnessFilter.setValue(brightnessProperty.value, forKey: brightnessProperty.key)
if let outputImage = self.blurFilter.outputImage {
self.brightnessFilter.setValue(outputImage, forKey: kCIInputImageKey)
} else {
self.brightnessFilter.setValue(self.currentImage, forKey: kCIInputImageKey)
}
outputImage = self.brightnessFilter.outputImage
}
if let cropProperty = self.allFilters[self.kCICrop]?.first {
self.cropFilter.setValue(cropProperty.value, forKey: cropProperty.key)
if let outputImage = self.brightnessFilter.outputImage {
self.cropFilter.setValue(outputImage, forKey: kCIInputImageKey)
} else if let outputImage = self.blurFilter.outputImage {
self.cropFilter.setValue(outputImage, forKey: kCIInputImageKey)
} else {
self.cropFilter.setValue(self.currentImage, forKey: kCIInputImageKey)
}
outputImage = self.cropFilter.outputImage
}
if let image = outputImage {
let openGLContext = EAGLContext(api: .openGLES2)
let context = CIContext(eaglContext: openGLContext!)
if let cgimg = context.createCGImage(image, from: image.extent) {
let processedImage = UIImage(cgImage: cgimg)
DispatchQueue.main.async {
self.imageView.image = processedImage
}
}
} else {
DispatchQueue.main.async {
self.imageView.image = self.selectedImage
}
}
}
} else {
DispatchQueue.main.async {
self.imageView.image = self.selectedImage
}
}
}
然后像这样应用滤镜(模糊滤镜)
@IBAction func sliderValueChanged(_ sender: UISlider) {
if sender.value == 0 {
_ = allFilters.removeValue(forKey: kCIGaussianBlur)
} else {
allFilters[kCIGaussianBlur] = [kCIInputRadiusKey: sender.value]
}
if allFilters[kCICrop]?.first == nil {
allFilters[kCICrop] = ["inputRectangle": CIVector(cgRect: self.currentImage.extent)]
}
applyAvailableFilters()
}
答案 1 :(得分:0)
您可能应该持有对原始图像的引用,而不仅仅是将过滤器应用于UIImageView
中当前显示的任何图像,当您应用过滤器然后将输出图像设置为{{1并且无法检索输入图像。另一种选择是保留对UIImageView
的引用。为此,您应该声明一个属性:
CIFilter
然后,您可以在应用过滤器中使用此过滤器并恢复为原始方法。
let noirFilter = CIFilter(name: "CIPhotoEffectNoir")!
或者更理想的是:
@IBAction func BW_Flt_Bt_Tapped(sender: UIButton) {
let beginImage = CIImage(image: imagePreview.image!)
noirFilter.setValue(beginImage, forKey: kCIInputImageKey)
let filteredImage = noirFilter.valueForKey(kCIOutputImageKey) as! CIImage
imagePreview.image = UIImage(CIImage: filteredImage)
self.orgImgBt.hidden = false
self.orgImgL.hidden = false
BWFlt.hidden = true
BWLbl.hidden = true
}
@IBAction func orgImgPressed(sender: UIButton) {
let inputImage = noirFilter.valueForKey(kCIInputImageKey) as! CIImage
imagePreview.image = UIImage(CIImage: inputImage)
orgImgBt.hidden = true
orgImgL.hidden = true
`enter code here` BWFlt.hidden = false
`enter code here`BWLbl.hidden = false
print("button was pressed")
}
其中@IBAction func orgImgPressed(sender: UIButton) {
imagePreview.image = self.currentInputImage
orgImgBt.hidden = true
orgImgL.hidden = true
`enter code here` BWFlt.hidden = false
`enter code here`BWLbl.hidden = false
print("button was pressed")
}
是对您要应用过滤器的currentInputImage
的引用。