captureStillImageAsynchronously Issue

时间:2018-02-01 17:41:35

标签: ios swift avfoundation

我目前遇到了AVCaptureStillImageOutput的问题,当我尝试拍照时,图片当前为零。我目前对错误修复的尝试已经发现captureStillImage异步方法根本没有被调用,我还没能测试样本缓冲区是否为零。我使用这种方法将摄像机图像输入另一种将摄像机图像和另一个图像组合成单个图像的方法。在最后一个方法中线程失败。当我尝试从捕获方法检查图像时,它不可用。我需要做些什么才能使相机捕捉工作?

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
}

1 个答案:

答案 0 :(得分:0)

If you use async method the function will return a wrong value, because the async call is still in progress. You can use a completion block, like that:

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)
    }
}

How to use it:

capturePhotoOutput
{ (image) in

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

  //Your code

}

Edit:

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
}