2017-09-28 19 views
6

私は私のアプリでカスタムカメラを持って、それがうまく働いたが、私はこのエラーを取得しています新しいアップデート後:カスタムカメラiOSの11.0スウィフト4. Updateエラーで写真を撮る

'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer:)' was deprecated in iOS 11.0: Use -[AVCapturePhoto fileDataRepresentation] instead.

これはラインでありますどこでそのエラーを取得しています:

guard let imageData = 
     AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else { 
      return 
    } 

これは私の全体の機能(必要な場合)である:

//Take pic function 
func photoOutput(_ captureOutput: AVCapturePhotoOutput, 
       didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, 
       previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, 
       resolvedSettings: AVCaptureResolvedPhotoSettings, 
       bracketSettings: AVCaptureBracketedStillImageSettings?, 
       error: Error?) { 


    // Make sure we get some photo sample buffer 
    guard error == nil, 
     let photoSampleBuffer = photoSampleBuffer else { 
      print("Error capturing photo: \(String(describing: error))") 
      return 
    } 
    // Convert photo same buffer to a jpeg image data by using // AVCapturePhotoOutput 
    guard let imageData = 
     AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else { 
      return 
    } 

    let dataProvider = CGDataProvider(data: imageData as CFData) 

    let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.absoluteColorimetric) 


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

    self.tempImageView.image = image 

} 

マイguestiをオンになっています:代わりに何を使用して機能させるべきですか?

ありがとうございます。 iOSの11で

答えて

11

、あなたはこのように使用する必要があります。

@available(iOS 11.0, *) 
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { 
     let imageData = photo.fileDataRepresentation() 
} 
4

おかげ@Viniアプリケーション、私はそれが私のために働いていたコードを試してみました、私は、人々を助ける希望を撮影して処理するための私のコードを掲載誰が自分のアプリで同様の機能を必要とするのか。

まずセットアップに必要なデバイスをキャプチャあなたのビデオが、それはここでGoogle検索すると、https://gist.github.com/tad-iizuka/fc35bc7835920c0b8b84e316f83e3a40

はあなたがいずれかの設定の写真を設定し、トップ

... 
var photoSetting = AVCapturePhotoSettings() 
... 

photoSettingを定義する必要があることを確認します例です。 viewDidLoad()又はviewWillAppear()

  // Configure camera 
     photoSetting = AVCapturePhotoSettings.init(format: [AVVideoCodecKey: AVVideoCodecType.jpeg]) 
     photoSetting.isAutoStillImageStabilizationEnabled = true 
     photoSetting.flashMode = .off 

そしてPに次の関数を使用しますこの関数にバッファリングされた画像データ

 func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { 

    // Check if there is any error in capturing 
    guard error == nil else { 
     print("Fail to capture photo: \(String(describing: error))") 
     return 
    } 

    // Check if the pixel buffer could be converted to image data 
    guard let imageData = photo.fileDataRepresentation() else { 
     print("Fail to convert pixel buffer") 
     return 
    } 

    // Check if UIImage could be initialized with image data 
    guard let capturedImage = UIImage.init(data: imageData , scale: 1.0) else { 
     print("Fail to convert image data to UIImage") 
     return 
    } 

    // Get original image width/height 
    let imgWidth = capturedImage.size.width 
    let imgHeight = capturedImage.size.height 
    // Get origin of cropped image 
    let imgOrigin = CGPoint(x: (imgWidth - imgHeight)/2, y: (imgHeight - imgHeight)/2) 
    // Get size of cropped iamge 
    let imgSize = CGSize(width: imgHeight, height: imgHeight) 

    // Check if image could be cropped successfully 
    guard let imageRef = capturedImage.cgImage?.cropping(to: CGRect(origin: imgOrigin, size: imgSize)) else { 
     print("Fail to crop image") 
     return 
    } 

    // Convert cropped image ref to UIImage 
    imageToSave = UIImage(cgImage: imageRef, scale: 1.0, orientation: .down) 
    UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil) 

    // Stop video capturing session (Freeze preview) 
    captureSession.stopRunning() 
} 

をrocess、画素バッファはphotoSettingによって指定された形式の画像データに変換し、次いで必要なサイズにトリミングされます。

あなたは2番目の写真が撮影されたことで、アプリケーションが私を言ってクラッシュしたため、変数は、私のために動作しませんでしたように、上部まで

@IBAction func onTakePhoto(_ sender: UIButton) { 
    if let videoConnection = videoOutput.connection(with: AVMediaType.video) { 
     // Adjust the orientaion of captured image 
     let capturePhotoSetting = AVCapturePhotoSettings.init(from: photoSetting) 
     videoConnection.videoOrientation = (previewLayer.connection?.videoOrientation)! 
     // Save captured photo to system album 
     self.videoOutput.capturePhoto(with: capturePhotoSetting, delegate: self) 
    } 
} 
+0

定義写真植字上のキャプチャ機能を呼び出すためにIBのボタンを作成することができます写真の設定を使用できませんでした。 AVCapturePhotoSettingsは、あなたが写真を撮るたびにIBActionや他の機能で "let"(varではなく)で定義することができます。 – VagueExplanation

+1

こんにちは、@VagueExplanation、返信いただきありがとうございますが、私はまさにクラッシュ(AVCapturePhotoSettingをインスタンス化するたびに聞こえるのですか?投稿されたコードは、私のアプリの「写真ブース」ビューで働いたので、基本的に写真を撮って満足している次のビューに進むことができます。写真はアルバムに保存され、アプリはiPadで動作します。それが助けになることを願っていますか?あるいは、議論の詳細を提供することができますか? –

+0

ああ、大丈夫です。どのように使用するかによって異なります。 – VagueExplanation

関連する問題