2017-03-24 7 views
0

私はtakePhotoボタンで写真をキャプチャすることができますが、AVCapturePhotoOutputにメンバー 'captureStillImageAsynchronously'がないと言ってtakePhotoにエラーがあります。AVCapturePhotoOutputにメンバー 'captureStillImageAsynchronously'がありません

のViewController

import UIKit 
    import AVFoundation 
    import CoreImage 

@available(iOS 10.0, *) 
class ViewController: UIViewController { 

var captureSession = AVCaptureSession() 
var sessionOutput = AVCapturePhotoOutput() 
var previewLayer = AVCaptureVideoPreviewLayer() 


@IBOutlet weak var imageViewReal: UIImageView! 
@IBOutlet weak var cameraView: UIView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 



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

    let deviceSession = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInDuoCamera, .builtInTelephotoCamera, .builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .unspecified) 

    for device in (deviceSession?.devices)! { 


     if (device as AnyObject).position == AVCaptureDevicePosition.front { 
      do { 
       let input = try AVCaptureDeviceInput(device: device) 

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

        if captureSession.canAddOutput(sessionOutput){ 
        captureSession.addOutput(sessionOutput) 

         previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
         previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
         previewLayer.connection.videoOrientation = .portrait 

         cameraView.layer.addSublayer(previewLayer) 
         cameraView.addSubview(takePhoto) 

         previewLayer.position = CGPoint (x: self.imageViewReal.frame.width/2, y: self.imageViewReal.frame.height/2) 
         previewLayer.bounds = imageViewReal.frame 
         captureSession.startRunning() 

        } 
       } 
      } 
      catch { 
      print("Error") 
      } 
     } 
    } 
} 


@IBAction func takePhoto(_ sender: Any) { 
    if let videoConnection = sessionOutput.connection(withMediaType: AVMediaTypeVideo) { 
     sessionOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { 
     buffer, Error in 
      let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer) 

      UIImageWriteToSavedPhotosAlbum(UIImage(data : imageData!)!, nil, nil, nil) 
     }) 
    } 
} 
} 

答えて

1

あなたのアプリがiOS9と互換性がある場合、AVCaptureStillImageOutput使用します。■メソッドの方法あなたのアプリがiOS10から始まる場合、あなたはAVCapturePhotoOutputを使用する必要があります

func captureStillImageAsynchronously(from connection: AVCaptureConnection!, 
        completionHandler handler: ((CMSampleBuffer?, Error?) -> Void)!) 

を '

open func capturePhoto(with settings: AVCapturePhotoSettings, delegate: AVCapturePhotoCaptureDelegate) 

だから、var sessionOutput = AVCapturePhotoOutput()var sessionOutput = AVCaptureStillImageOutput()になるはずです。

お試しください。

+0

あなたの努力のために余裕を持っていますが、私もline#54の 'cameraView.addSubview(takePhoto)' errorr: 'Any型の値をUIviewに変換できません。 –

+0

あなたの努力のためにDrawinをやっていますが、 54行目のエラーです。cameraView.addSubview(takePhoto)errorr:Any型の値をUIviewに変換できません –

関連する問題