2016-12-13 11 views
3

私はカスタムカメラアプリを開発中です。このチュートリアルでは、iOS 10では廃止予定のAVCaptureStillImageOutputを使用しています。私はカメラを設定しています。ここでAVCapturePhotoOutputを使用して写真swift + xcodeをキャプチャできません

私は私がここに答え0123を経てい

@IBAction func clickCapture(_ sender: UIButton) { 

if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) { 
    // This is where I need help 
    } 
} 

を助けが必要な場所ですカメラに

import UIKit 
import AVFoundation 

var cameraPos = "back" 

class View3: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate { 


@IBOutlet weak var clickButton: UIButton! 
@IBOutlet var cameraView: UIView! 
var session: AVCaptureSession? 
var stillImageOutput: AVCapturePhotoOutput? 
var videoPreviewLayer: AVCaptureVideoPreviewLayer? 

override func viewDidLoad() { 
    super.viewDidLoad()   
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    clickButton.center.x = cameraView.bounds.width/2 
    loadCamera() 
} 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
} 

@IBAction func clickCapture(_ sender: UIButton) { 

    if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) { 
     // This is where I need help 
     } 
} 

@IBAction func changeDevice(_ sender: UIButton) { 
    if cameraPos == "back" 
    {cameraPos = "front"} 

    else 
    {cameraPos = "back"} 


    loadCamera() 
} 

func loadCamera() 
{ 
    session?.stopRunning() 
    videoPreviewLayer?.removeFromSuperlayer() 

    session = AVCaptureSession() 
    session!.sessionPreset = AVCaptureSessionPresetPhoto 

    var backCamera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front) 

    if cameraPos == "back" 
    { 
     backCamera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .back) 
    } 

    var error: NSError? 
    var input: AVCaptureDeviceInput! 
    do { 
     input = try AVCaptureDeviceInput(device: backCamera) 
    } catch let error1 as NSError { 
     error = error1 
     input = nil 
     print(error!.localizedDescription) 
    } 

    if error == nil && session!.canAddInput(input) { 
     session!.addInput(input) 

     stillImageOutput = AVCapturePhotoOutput() 

if session!.canAddOutput(stillImageOutput) { 
      session!.addOutput(stillImageOutput) 
      videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session) 
      videoPreviewLayer?.frame = cameraView.bounds 
      videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
      videoPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait 

      cameraView.layer.addSublayer(videoPreviewLayer!) 
      session!.startRunning() 

     }  } 
} 
} 

を持って私の完全な図であり、 しかし、このコードにこのコードを組み込む方法がわかりません。新しいクラスを宣言することになります。

答えて

5

あなたはほぼそこにいます。 AVCapturePhotoOutput

として出力のための

は、より多くの助けのためのAVCapturePhotoOutputdocumentationをチェックしてください。

これは写真を撮影する手順です。

  1. AVCapturePhotoOutputオブジェクトを作成します。そのプロパティを使用して サポートされているキャプチャ設定を決定し、特定の機能を有効にする (ライブ写真をキャプチャするかどうかなど)。
  2. を作成して特定のキャプチャの機能と設定を選択します(たとえば、画像安定化またはフラッシュを有効にするには )。
  3. フォト設定オブジェクトを capturePhoto(with:delegate:)メソッドに渡して、AVCapturePhotoCaptureDelegateプロトコルを実装するデリゲートオブジェクト と一緒にイメージをキャプチャします。写真 キャプチャ出力は、キャプチャプロセス中に重要な イベントを通知するために代理人を呼び出します。

あなたのclickCaptureメソッドではこれを以下のコードにして、あなたのクラスで委任することを忘れて実装するのを忘れないでください。 AVCaptureStillImageOutput

として出力のための

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 
self.cameraOutput.capturePhoto(with: settings, delegate: self) 

あなたはビデオ接続から写真をスナップする場合。以下の手順に従います。

ステップ1:

  • stillImageOutputcaptureStillImageAsynchronouslyFromConnection関数を呼び出し、写真をキャプチャ:接続

    if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) { 
        // ... 
        // Code for photo capture goes here... 
    } 
    

    ステップ2を取得します。

  • sampleBufferは、キャプチャされるデータを表します。

stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in 
    // ... 
    // Process the image data (sampleBuffer) here to get an image file we can put in our captureImageView 
}) 

ステップ3:プロセスイメージデータを

  • 私たちは、で終わるためにsampleBufferで見つかった画像データを処理するために、いくつかの手順を実行する必要があります私たちのcaptureImageViewに挿入することができ、私たちのアプリのどこかで簡単に使用できるUIImage。

if sampleBuffer != nil { 
    let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 
    let dataProvider = CGDataProviderCreateWithCFData(imageData) 
    let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault) 
    let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right) 
    // ... 
    // Add the image to captureImageView here... 
} 

ステップ4:あなたの必要性の写真ギャラリーに画像を保存したりするためにその画像ビューで


示してどちらかに基づいて、画像に

を保存もっと詳しくはこちらをご覧くださいCreate custom camera view guide写真をスナップ

+1

captureStillImageAsynchronouslyFromConnection関数はAVCapturePhotoOutputのメンバーではなく、AVCaptureStillImageOutputである – user2238284

+0

'captureStillImageAsynchronouslyFromConnection'はカスタムカメラでビデオから写真を撮ることです。 AVCapturePhotoOutputは、直接 – Bluewings

+0

を撮影するのに使用されていますが、AVCapturePhotoOutputのオブジェクトとしてstillImageOutputを持っていて、stillImageOutput?stillStageImageAsynchronouslyFromConnection(videoConnection、completionHandler:{(sampleBuffer、error) - > Void in Void in にはエラー – user2238284

関連する問題