BradLarson / GPUImage3

GPUImage 3 is a BSD-licensed Swift framework for GPU-accelerated video and image processing using Metal.
BSD 3-Clause "New" or "Revised" License
2.68k stars 330 forks source link

Fatal error in Source/PictureInput.swift: Unexpectedly found nil while unwrapping an Optional value #89

Open mycroftcanner opened 4 years ago

mycroftcanner commented 4 years ago

Trying to use GPUImage3 inside a ASNetworkImageNode's imageModificationBlock:

     node.imageModificationBlock = { image, _ in
                            let luminance = Luminance()

                            let exposure = ExposureAdjustment()
                            exposure.exposure = -0.3

                            return image.filterWithPipeline { input, output in
                                input --> luminance --> exposure --> output
                            }
                        }

It crashes sometimes with the following error:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/mc/Test/GPUImage3/framework/Source/PictureInput.swift, line 20

    #if canImport(UIKit)
    public convenience init(image:UIImage, smoothlyScaleOutput:Bool = false, orientation:ImageOrientation = .portrait) {
        self.init(image: image.cgImage!, smoothlyScaleOutput: smoothlyScaleOutput, orientation: orientation)
    }
mycroftcanner commented 4 years ago

Is the filterWithPipeline holding a weak reference to image?

mycroftcanner commented 4 years ago

The solution is to make sure image.cgImage isn't nil :

    node.imageModificationBlock = { image, _ in
         guard image.cgImage != nil else { return image }

          let luminance = Luminance()

          let exposure = ExposureAdjustment()
          exposure.exposure = -0.3

          return image.filterWithPipeline { input, output in
                    input --> luminance --> exposure --> output
         }
}