从函数获取UIImage [英] Get UIImage from function
本文介绍了从函数获取UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个函数将滤镜应用于照片。为什么函数总是返回nil?
func getImage()-> UIImage? {
let openGLContext = EAGLContext(api:.openGLES2)
let context = CIContext(eaglContext:openGLContext!)
let coreImage = UIImage(cgImage:image.cgImage!)
filter.setValue(coreImage,forKey:kCIInputImageKey)
filter.setValue(1,forKey:kCIInputContrastKey)
如果让outputImage = filter.value(forKey:kCIOutputImageKey )一样吗? CIImage {
let output = context.createCGImage(outputImage,from:outputImage.extent)
return UIImage(cgImage:output!)
}
返回nil
}
解决方案
使用<$ c $ iOS
中的c> coreImage 。您必须考虑coreImage类,例如
I wrote a function to apply the filter to a photo. Why the function always returns nil?
func getImage() -> UIImage? {
let openGLContext = EAGLContext(api: .openGLES2)
let context = CIContext(eaglContext: openGLContext!)
let coreImage = UIImage(cgImage: image.cgImage!)
filter.setValue(coreImage, forKey: kCIInputImageKey)
filter.setValue(1, forKey: kCIInputContrastKey)
if let outputImage = filter.value(forKey: kCIOutputImageKey) as? CIImage {
let output = context.createCGImage(outputImage, from: outputImage.extent)
return UIImage(cgImage: output!)
}
return nil
}
解决方案
When you working with coreImage
in iOS
.You have to considered coreImage classes such as CIFilter, CIContext, CIVector, and CIColor and the input image should be a CIImage
which holds the image data. It can be created from a UIImage.
Note: You haven't mentioned about your filter definition or custom filter that you using.From your code, I can see you trying to change the contrast
of an input image.So, I am testing the code using CIColorControls
filter to adjust the contrast.
func getImage(inputImage: UIImage) -> UIImage? {
let openGLContext = EAGLContext(api: .openGLES2)
let context = CIContext(eaglContext: openGLContext!)
let filter = CIFilter(name: "CIColorControls")
let coreImage = CIImage(image: filterImage.image!)
filter?.setValue(coreImage, forKey: kCIInputImageKey)
filter?.setValue(5, forKey: kCIInputContrastKey)
if let outputImage = filter?.value(forKey: kCIOutputImageKey) as? CIImage {
let output = context.createCGImage(outputImage, from: outputImage.extent)
return UIImage(cgImage: output!)
}
return nil
}
You can call the above func like below.
filterImage.image = getImage(inputImage: filterImage.image!)
Output:
这篇关于从函数获取UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文