如何从AV Foundation返回图像仍然捕获并将其添加到图形上下文中以获取屏幕截图?

Big*_*337 3 avfoundation uiimage ios swift

我有一个相机预览,效果很好.我想截取它的屏幕截图及其上的所有内容.但是,由于通常的截图方法: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)
                }


            })


        })
    }
Run Code Online (Sandbox Code Playgroud)

我想返回定义的图像,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)
}
Run Code Online (Sandbox Code Playgroud)

这将让我的屏幕截图包含最近捕获的图像以及添加了UIGraphicsGetCurrentContext()的所有内容!

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

Rus*_*tin 5

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

假设这个代码都在同一个类中进行,你可以使image成为一个属性

class CameraViewController: UIViewController {
    var image: UIImage!
    ...
Run Code Online (Sandbox Code Playgroud)

然后进去 captureStillImageAsynchronouslyFromConnection

代替

let image:UIImage = UIImage( data: data)!
Run Code Online (Sandbox Code Playgroud)

使用

self.image = UIImage( data: data)!
Run Code Online (Sandbox Code Playgroud)

然后在downloadButton使用中

var imageView = UIImageView(frame: previewView.frame)
imageView.image = self.image
previewView.addSubview(imageView)
previewView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
Run Code Online (Sandbox Code Playgroud)

然后,您可以根据需要删除imageView

imageView.removeFromSuperview()
Run Code Online (Sandbox Code Playgroud)