当前位置 : 主页 > 手机开发 > ios >

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

来源:互联网 收集:自由互联 发布时间:2021-06-11
我有一个相机预览,效果很好.我想截取它的屏幕截图及其上的所有内容.但是,由于通常的截图方法:CALayer的renderInContext不会从相机渲染内容,我需要单独添加它. 我在视图中使用此功能控
我有一个相机预览,效果很好.我想截取它的屏幕截图及其上的所有内容.但是,由于通常的截图方法: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)
                }


            })


        })
    }

我想返回使用let image定义的图像:UIImage = UIImage(数据:数据)!
以便通过以下功能访问它

@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函数中已包含的完成处理程序块中来完成此操作,但我不太清楚如何执行此操作.有什么建议?

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

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

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()
网友评论