2017-03-02 2 views
1

私はSwift 3、Xcode 8.2を使用しています。Swift 3 - カスタムカメラビュー - ボタンをクリックした後に撮影された写真の静止画像を表示

私はシャッターとして機能したいビデオフィードとボタンを表示するカスタムカメラビューを持っています。ユーザーがボタンをタップすると、撮影した画像を画面に表示します。 (例えばSnapchatやFacebookメッセンジャースタイルのカメラの動作など)

ここでは私のコードです:

import UIKit 
import AVFoundation 

class CameraVC: UIViewController, AVCapturePhotoCaptureDelegate { 

    // this is where the camera feed from the phone is going to be displayed 
    @IBOutlet var cameraView : UIView! 

    var shutterButton : UIButton = UIButton.init(type: .custom) 

    // manages capture activity and coordinates the flow of data from input devices to capture outputs. 
    var capture_session = AVCaptureSession() 

    // a capture output for use in workflows related to still photography. 
    var session_output = AVCapturePhotoOutput() 

    // preview layer that we will have on our view so users can see the photo we took 
    var preview_layer = AVCaptureVideoPreviewLayer() 

    // still picture image is what we show as the picture taken, frozen on the screen 
    var still_picture_image : UIImage! 

    ... //more code in viewWillAppear that sets up the camera feed 

    // called when the shutter button is pressed 
    func shutterButtonPressed() { 

     // get the actual video feed and take a photo from that feed 
     session_output.capturePhoto(with: AVCapturePhotoSettings.init(format: [AVVideoCodecKey : AVVideoCodecJPEG]), delegate: self as AVCapturePhotoCaptureDelegate) 
    } 

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

     // take the session output, get the buffer, and create an image from that buffer 
     if let sampleBuffer = photoSampleBuffer, 
      let previewBuffer = previewPhotoSampleBuffer, 
      let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) { 

      print("Here") // doesn't get here 

     } 

    } 

このコードを実行しているとき、「ここ」に印刷していないようだと、私はどんなスウィフトを見つけることができませんこの画像を表示する方法に関する3つのチュートリアル。私はimageDataを受け取り、それをstill_picture_imageに割り当てて、それをカメラのフィードの上に何とかオーバーレイしたいと思っています。

どのようなヘルプまたは正しい方向のポイントが大きな助けになるでしょう。私のコードに以下を追加した後

EDIT

 if let error = error { 
      print(error.localizedDescription) 
     } 

しかし、私はまだエラーが印刷されません。

+0

あなただけのキャプチャとUIImageViewを追加することができますイメージ右? –

+0

理論的にはyesですが、 "Here"という単語は私のコードでは印刷されていないので、 'if'ステートメントを過ぎているようには見えません。 – noblerare

+0

'captureSession?startRunning()'を呼び出しましたか?ちょっと全長コードを投稿できますか? –

答えて

0

エラーがスローされてプリントアウトし、デリゲートメソッドに次のコードを追加します。

if let error = error { 
    print(error.localizedDescription) 
} 

あなたはあなたのエラーが解決得れば、私はこのポストは画像を抽出するためのお手伝いをすべきだと思う:Taking photo with custom camera Swift 3

+0

さて、元の投稿を更新しました。 – noblerare

0

さて、私は問題を理解しました:

まず、UIImageViewをストーリーボードにドラッグし、画面全体を占有させます。シャッターボタンを押した後に静止画が表示されます。

コード内にその変数を作成してリンクします。

@IBOutlet weak var stillPicture : UIImageView! 

その後、viewDidLoadにあなたがカメラのビューの上にUIImageViewを挿入していることを確認してください。キャプチャデリゲートで、そして、

func shutterButtonPressed() { 

    let settings = AVCapturePhotoSettings() 

    let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first! 
    let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType, 
         kCVPixelBufferWidthKey as String: 160, 
         kCVPixelBufferHeightKey as String: 160, 
         ] 
    settings.previewPhotoFormat = previewFormat 
    session_output.capturePhoto(with: settings, delegate: self) 
} 

self.view.insertSubview(stillPicture, aboveSubview: your_camera_view) 

これは、シャッターボタンがクリックされたときに呼び出される関数です

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { 
     if let error = error { 
      print(error.localizedDescription) 
     } 

     // take the session output, get the buffer, and create an image from that buffer 
     if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) { 
      // this is the image that the user has taken! 
      let takenImage : UIImage = UIImage(data: dataImage)! 
      stillPicture?.image = takenImage     
     } else { 
      print("Error setting up photo capture") 
     } 
} 
関連する問題