我有一个函数来旋转 UIImages 上的色调,该函数与同一类中的其他函数一起工作,但在 captureOutput 函数(与 AVCaptureVideoDataOutput 一起使用)中,相同的图像对象使我的应用程序崩溃,并显示“在展开可选值时意外发现 nil” . 我调用它的方式看起来没有什么不同,所以它可能与 captureOutput 函数及其允许的功能有关?
在 viewController 类声明之外:
var image:UIImage? = nil
var imageSource:UIImage? = nil
var imageView:UIImageView? = nil;
var hueDeg:CGFloat = 0.00
功能:
func rotateHue(with source: UIImage, rotatedByHue deltaHueRadians: CGFloat) -> UIImage {
// Create a Core Image version of the image.
let sourceCore = CIImage(cgImage: (source.cgImage)!) // crashes here
// Apply a CIHueAdjust filter
let hueAdjust = CIFilter(name: "CIHueAdjust")
hueAdjust?.setDefaults()
hueAdjust?.setValue(sourceCore, forKey: "inputImage")
hueAdjust?.setValue(deltaHueRadians, forKey: "inputAngle")
let resultCore = hueAdjust?.value(forKey: "outputImage") as? CIImage
// Convert the filter output back into a UIImage.
let context = CIContext(options: nil)
let resultRef = context.createCGImage(resultCore!, from: (resultCore?.extent)!)
let result = UIImage(cgImage: resultRef!)
//CGImageRelease(resultRef)
return result
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection){
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let cameraImage = CIImage(cvPixelBuffer: pixelBuffer!)
image = UIImage(ciImage: cameraImage)
image = rotateHue(with:image!, rotatedByHue:hueDeg)
DispatchQueue.main.async(){
imageView!.image = image
}
}
这是我调用 rotateHue 的另一个函数,它不会崩溃。这是一个由计时器调用的函数:
@objc func cycleHue(){
hueDeg += 0.2;
image = imageSource
image = rotateHue(with:image!, rotatedByHue:hueDeg)
imageView!.image = UIImage(cgImage: (image?.cgImage)!, scale: 1.0, orientation: UIImageOrientation.right)
}