captureStillImageAsynchronously 问题

captureStillImageAsynchronously Issue

我目前遇到 AVCaptureStillImageOutput 问题,当我尝试拍照时图像当前为零。我目前的错误修复尝试发现 captureStillImageAsynchronously 方法根本没有被调用,我还无法测试样本缓冲区是否为 nil。我正在使用这种方法将相机图像输入另一种方法,将相机图像和另一幅图​​像组合成一张图像。线程在最后一个方法中失败。当我尝试检查来自捕获方法的图像时,它不可用。我需要做什么才能让相机捕捉正常工作?

public func capturePhotoOutput()->UIImage
{
    var image:UIImage = UIImage()
    if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo)
    {
        print("Video Connection established ---------------------")
        stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
            if (sampleBuffer != nil)
            {
                print("Sample Buffer not nil ---------------------")
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProvider(data: imageData! as CFData)
                let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
                let camImage = UIImage(cgImage: cgImageRef!, scale: CGFloat(1.0), orientation: UIImageOrientation.right)
                image = camImage
            }
            else
            {
                print("nil sample buffer ---------------------")
            }
        })

    }
    if (stillImageOutput?.isCapturingStillImage)!
    {
        print("image capture in progress ---------------------")
    }
    else
    {
        print("capture not in progress -------------------")
    }
    return image
}

编辑:添加了以下使用相机图像的方法。

func takePicture()-> UIImage
{
    /*
    videoComponent!.getVideoController().capturePhotoOutput
    { (image) in
            //Your code
            guard let topImage = image else
            {
                print("No image")
                return
            }
    }
    */
    let topImage = videoComponent!.getVideoController().capturePhotoOutput() //overlay + Camera
    let bottomImage = captureTextView() //text

    let size = CGSize(width:(topImage.size.width),height:(topImage.size.height)+(bottomImage.size.height))

    UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

    topImage.draw(in: CGRect(x:0, y:0, width:size.width, height: (topImage.size.height)))
    bottomImage.draw(in: CGRect(x:(size.width-bottomImage.size.width)/2, y:(topImage.size.height), width: bottomImage.size.width,  height: (bottomImage.size.height)))

    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return newImage
}

如果您使用异步方法,该函数将 return 一个错误的值,因为异步调用仍在进行中。你可以使用一个完成块,像这样:

public func capturePhotoOutput(completion: (UIImage?) -> ())
{
    if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo)
    {
        print("Video Connection established ---------------------")
        stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
            if (sampleBuffer != nil)
            {
                print("Sample Buffer not nil ---------------------")
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProvider(data: imageData! as CFData)
                let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
                let camImage = UIImage(cgImage: cgImageRef!, scale: CGFloat(1.0), orientation: UIImageOrientation.right)
                completion(camImage)
            }
            else
            {
                completion(nil)
            }
        })
    }
    else
    {
        completion(nil)
    }
}

使用方法:

capturePhotoOutput
{ (image) in

  guard let topImage = image else{
      print("No image")
      return
  }

  //Your code

}

编辑:

func takePicture()
{
    videoComponent!.getVideoController().capturePhotoOutput
    { (image) in

        guard let topImage = image else
        {
            print("No image")
            return
        }

        let bottomImage = self.captureTextView() //text

        let size = CGSize(width:(topImage.size.width),height:(topImage.size.height)+(bottomImage.size.height))

        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

        topImage.draw(in: CGRect(x:0, y:0, width:size.width, height: (topImage.size.height)))
        bottomImage.draw(in: CGRect(x:(size.width-bottomImage.size.width)/2, y:(topImage.size.height), width: bottomImage.size.width,  height: (bottomImage.size.height)))

        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        self.setPicture(image: newImage)
    }
}

func setPicture(image:UIImage)
{
    //Your code after takePicture
}