     /// - Parameter sampleBuffer: sample buffer input
     /// - Returns: UIImage from the sample buffer, otherwise nil
     public func uiimage(withSampleBuffer sampleBuffer: CMSampleBuffer) -> UIImage? {
+        guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
+            return nil
+        }
+       
+        let ciimage = CIImage(cvPixelBuffer: pixelBuffer)
         var sampleBufferImage: UIImage? = nil
-        if let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
-            let ciimage = CIImage(cvPixelBuffer: pixelBuffer)
-            if let cgimage = self.createCGImage(ciimage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(pixelBuffer), height: CVPixelBufferGetHeight(pixelBuffer))) {
-                sampleBufferImage = UIImage(cgImage: cgimage)
-            }
+        if let cgimage = self.createCGImage(ciimage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(pixelBuffer), height: CVPixelBufferGetHeight(pixelBuffer))) {
+            sampleBufferImage = UIImage(cgImage: cgimage)
         }
         return sampleBufferImage
     }

    /// - Returns: UIImage from the pixel buffer, otherwise nil
    public func uiimage(withPixelBuffer pixelBuffer: CVPixelBuffer) -> UIImage? {
        var pixelBufferImage: UIImage? = nil
        if CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.readOnly) == kCVReturnSuccess {
            let ciimage = CIImage(cvPixelBuffer: pixelBuffer)
            if let cgimage = self.createCGImage(ciimage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(pixelBuffer), height: CVPixelBufferGetHeight(pixelBuffer))) {
                pixelBufferImage = UIImage(cgImage: cgimage)
            }
            CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.readOnly)
        }
        return pixelBufferImage
    }


    @available(iOS 11.0, *)
    public func createPixelBuffer(fromMTLTexture mtlTexture: MTLTexture, withOrientation orientation: CGImagePropertyOrientation, pixelBufferPool: CVPixelBufferPool) -> CVPixelBuffer? {
        var updatedPixelBuffer: CVPixelBuffer? = nil
        guard CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pixelBufferPool, &updatedPixelBuffer) == kCVReturnSuccess else {
            return nil
        }
        
        if let updatedPixelBuffer = updatedPixelBuffer {
            // update orientation to match Metal's origin
            let ciImage = CIImage(mtlTexture: mtlTexture, options: nil)
            if let orientedImage = ciImage?.oriented(orientation) {
                CVPixelBufferLockBaseAddress(updatedPixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
                self.render(orientedImage, to: updatedPixelBuffer)
                CVPixelBufferUnlockBaseAddress(updatedPixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
                return updatedPixelBuffer
            }
        }
        return nil
