如何 return 来自 AV Foundation 的图像仍然捕获并将其添加到图形上下文以获取屏幕截图?

How to return image from AV Foundation still capture and add it to graphics context for a screenshot?

我有一个相机预览,效果很好。我想截取它以及上面的所有内容。但是,由于通常的截图方式:CALayer的renderInContext并没有渲染来自相机的内容,所以我需要单独添加。

我的视图控制器中有这个功能,可以捕获图像并将其保存到相机胶卷中。

 @IBAction func snapStillImage(sender: AnyObject) {
        print("snapStillImage")
        (self.previewView.layer as! AVCaptureVideoPreviewLayer).connection.enabled=false;


        dispatch_async(self.sessionQueue, {
            // Update the orientation on the still image output video connection before capturing.

            let videoOrientation =  (self.previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation

            self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = videoOrientation

            // Flash set to Auto for Still Capture
            ViewController.setFlashMode(AVCaptureFlashMode.Auto, device: self.videoDeviceInput!.device)



            self.stillImageOutput!.captureStillImageAsynchronouslyFromConnection(self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo), completionHandler: {
                (imageDataSampleBuffer: CMSampleBuffer!, error: NSError!) in

                if error == nil {
                    let data:NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
                    let image:UIImage = UIImage( data: data)!

                    let libaray:ALAssetsLibrary = ALAssetsLibrary()
                    let orientation: ALAssetOrientation = ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!
                    libaray.writeImageToSavedPhotosAlbum(image.CGImage, orientation: orientation, completionBlock: nil)

                    print("save to album")


                }else{
//                    print("Did not capture still image")
                    print(error)
                }


            })


        })
    }

我想return用let image:UIImage = UIImage( data: data)!定义的图像 以便可以通过以下函数访问它

@IBAction func downloadButton(sender: UIButton) {
    //Create the UIImage



    UIGraphicsBeginImageContextWithOptions(previewView.frame.size, false, 0.0)

    previewView.layer.renderInContext(UIGraphicsGetCurrentContext()!)

    //ALSO ADD image captured with captureStillImageAsynchronouslyFromConnection right here to the context.
    let image = UIGraphicsGetImageFromCurrentImageContext()



    //Save it to the camera roll

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

这将使我的屏幕截图包括最近捕获的图像以及顶部使用 UIGraphicsGetCurrentContext() 添加的所有内容!

我听说您通过将图像包含在已包含在 captureStillImageAsynchronouslyFromConnection 函数中的完成处理程序块中来执行此操作,但我不太确定如何执行此操作。有什么建议吗?

您无法渲染来自相机的内容,因为 CALayer 的 renderInContext 不起作用。

尽管你想工作,以下link可以帮助你。

Taking a screenshot when camera is on -iOS

https://developer.apple.com/library/ios/qa/qa1702/_index.html#//apple_ref/doc/uid/DTS40010192

您可以将捕获的静止图像作为 UIImage 添加为 previewView 的子视图。然后调用 renderInContext.

假设这段代码都在同一个 class 中进行,您可以使图像 属性 像

class CameraViewController: UIViewController {
    var image: UIImage!
    ...

然后在captureStillImageAsynchronouslyFromConnection

而不是

let image:UIImage = UIImage( data: data)!

使用

self.image = UIImage( data: data)!

然后在downloadButton中使用

var imageView = UIImageView(frame: previewView.frame)
imageView.image = self.image
previewView.addSubview(imageView)
previewView.layer.renderInContext(UIGraphicsGetCurrentContext()!)

如果需要,您可以删除 imageView

imageView.removeFromSuperview()