46

カスタムカメラの使用に取り組んでおり、最近、Swift 3 とともに Xcode 8 ベータにアップグレードしました。

var stillImageOutput: AVCaptureStillImageOutput?

ただし、次の警告が表示されます。

'AVCaptureStillImageOutput' は iOS 10.0 で廃止されました: 代わりに AVCapturePhotoOutput を使用してください

これはかなり新しいものなので、これに関する情報はあまりありません。これが私の現在のコードです:

var captureSession: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var previewLayer: AVCaptureVideoPreviewLayer?

func clickPicture() {

    if let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo) {

        videoConnection.videoOrientation = .portrait
        stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) -> Void in

            if sampleBuffer != nil {

                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProvider(data: imageData!)
                let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)

                let image = UIImage(cgImage: cgImageRef!, scale: 1, orientation: .right)

            }

        })

    }

}

を調べてみましたAVCapturePhotoCaptureDelegateが、使い方がよくわかりません。誰もこれを使用する方法を知っていますか? ありがとう。

4

7 に答える 7

16

iOS 11"photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {}" is deprecatedで。

次の方法を使用します。

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    let imageData = photo.fileDataRepresentation()
    if let data = imageData, let img = UIImage(data: data) {
        print(img)
    }
}
于 2018-05-18T07:22:13.927 に答える
3

デバイスとキャプチャ セッションの初期化を理解するのに役立つこのプロジェクトを GitHub で見つけました。

AVCapturePhotoOutput_test by inoue0426

于 2016-12-28T04:01:03.007 に答える