2017-03-09 24 views
1

カメラビュー(AVCapturePhotoOutput)を示すスクリーンショットを保存します。Swiftでカメラ(AVCapturePhotoOutput)を表示しているUIViewのみのスクリーンショットを取得できません

私はcameraView(カメラビューを表示)を設定し、UIButtonのような部分を持つ他のUIViewを追加しました。

私はそれを保存すると、カメラロールに保存された画像は単なる白い画像です。 (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); 

     } 
} 

UPDATE:CAShapeLayerを作る

追加のコードが、それはカメラビューを表示することができず、画像の保存が完了すると、画像はホワイトのみ絵です....

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); 
キャプチャセッションから
+0

へのUIView

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

AVCapturePhotoCaptureDelegate

セットデリゲートのnshotは、それがうまく動作しませんでした。このhttp://stackoverflow.com/a/42668262/5461400 –

+0

を試してみてください。コードを更新しましたので、確認してください。 – bao

+0

あなたはマスキングする必要はありませんでした。現在のコンテキストからイメージを取得する –

答えて

0

画像

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

スクリー自己

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

} 
+0

エラー "タイプ" AVCapturePhotoOutputの値に "output?.captureStillImageAsynchronously ..."という行に "captureStillImageAsynchronously"というメンバがありません...(最初に "AVCapturePhotoCaptureDelegate"を設定しました) – bao

+0

var出力:AVCaptureStillImageOutput?not AVCapturePhotoOutput –

+0

更新された回答を確認してください –

関連する問題