• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 无法仅在 Swift 中显示相机(AVCapturePhotoOutput)的 UIView 的屏幕截图

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:11:00 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想保存显示相机 View 的屏幕截图 (AVCapturePhotoOutput)。

我设置了cameraView(显示相机 View )并添加了其他具有UIButton等部分的UIView。

但是如果我保存它,保存到相机胶卷的图片只是一个白色 View 。 (当我尝试保存 self.view 的屏幕截图时,效果很好。)

我该如何解决?

class ViewController: UIViewController, AVCapturePhotoCaptureDelegate{

    //for camera
    var captureDevice : AVCaptureDevice!
    var captureSesssion: AVCaptureSession!
    var stillImageOutput: AVCapturePhotoOutput?
    var previewLayer: AVCaptureVideoPreviewLayer?
    var videoConnection:AVCaptureConnection!

    var topView: TopView! //on cameraView. It has some buttons and imageViews.
    var cameraView:UIView! //that shows camera's view

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = (UIColor.black)

        cameraView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height))
        self.view.addSubview(cameraView)

        captureSesssion = AVCaptureSession()
        stillImageOutput = AVCapturePhotoOutput()

        captureSesssion.sessionPreset = AVCaptureSessionPreset1280x720
        captureDevice = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)

        do {
            let input = try AVCaptureDeviceInput(device: captureDevice)

            if (captureSesssion.canAddInput(input)) {
                captureSesssion.addInput(input)

                if (captureSesssion.canAddOutput(stillImageOutput)) {
                    captureSesssion.addOutput(stillImageOutput)
                    captureSesssion.startRunning()

                    previewLayer = AVCaptureVideoPreviewLayer(session: captureSesssion)
                    previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect 
                    previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait 
                    previewLayer?.connection.automaticallyAdjustsVideoMirroring = false
                    previewLayer?.connection.isVideoMirrored = true
                    cameraView.layer.addSublayer(previewLayer!)

                    previewLayer?.position = CGPoint(x: self.view.frame.width / 2, y: self.view.frame.height / 2)
                    previewLayer?.bounds = self.view.frame
                }
            }
        }
        catch {
            print(error)
        }

        topView = TopView.init(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
        topView.viewController = self
        self.view.addSubview(topView)

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        let touch = touches.first!
        captureSesssion.stopRunning() //for taking still picture
        let layer = UIApplication.shared.keyWindow?.layer
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions((layer?.frame.size)!, false, scale);

        cameraView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let screenshot = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        //save the screenshot to camera roll        
        UIImageWriteToSavedPhotosAlbum(screenshot!, nil, nil, nil);

      }
}

更新:

增加了制作CAShapeLayer的代码,但是无法显示相机 View ,保存图片后,图片只是白色的图片......

cameraView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(cameraView)

let cropRectOverlay = CAShapeLayer() //added
cameraView.layer.mask = cropRectOverlay //added
cameraView.layer.addSublayer(cropRectOverlay) //added
...
previewLayer?.connection.automaticallyAdjustsVideoMirroring = false
previewLayer?.connection.isVideoMirrored = true
cameraView.layer.addSublayer(cropRectOverlay) //added
...

UIGraphicsBeginImageContextWithOptions(cameraView.bounds.size, false, 0); //added 
cameraView.layer.render(in: UIGraphicsGetCurrentContext()!) //added
let image = UIGraphicsGetImageFromCurrentImageContext(); //added
UIGraphicsEndImageContext(); //added

//When added resultImageView, it shows black view.          
//let resultImageView = UIImageView(frame: self.view.frame)
//resultImageView.image = image
//self.view.addSubview(resultImageView)

UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil);



Best Answer-推荐答案


来自捕获 session 的图像

let stillImageOutput = AVCaptureStillImageOutput()

stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if captureSession.canAddOutput(stillImageOutput) {
      captureSession.addOutput(stillImageOutput)
}


func captureImage() {
     let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo)
     stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(ingDataBuffer, error) in
         let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(ingDataBuffer)
         self.imgView.image = UIImage(data: imageData!)
    })
}

UIView 截图

UIGraphicsBeginImageContextWithOptions(cameraView.bounds.size, false, 0);
cameraView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

AVCapturePhotoCaptureDelegate

将委托(delegate)设置为自己

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: NSError?) {

    if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {
      print(image: UIImage(data: dataImage).size)
    }

}

关于ios - 无法仅在 Swift 中显示相机(AVCapturePhotoOutput)的 UIView 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42687723/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap