2017-09-14 121 views
3

我有一個正常的UIViewController,使用AVCapturePhoto我創建了一個自定義捕獲控制器。更新到Xcode 9後Swift編譯AVcode錯誤/ Swift 4

一切工作正常,直到我更新到Xcode 9並轉換我的代碼Swift 4,現在當我嘗試編譯我得到這個編譯錯誤:

Undefined symbols for architecture x86_64: "__T0So22AVCapturePhotoSettingsC12AVFoundation01_abC16SwiftNativeTypesACWP", referenced from: __T05Union16CameraControllerC18handleCapturePhotoyyF in CameraController.o "__T012AVFoundation39_AVCapturePhotoSettingsSwiftNativeTypesPAAE016availablePreviewc11PixelFormatG0Says6UInt32VGfg", referenced from: __T05Union16CameraControllerC18handleCapturePhotoyyF in CameraController.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

,並在錯誤托盤顯示此消息:

Apple Mach-O Linker Error Group

任何提示可能會導致這種情況?

更新的信息

這裏的錯誤日誌的Xcode的9打印屏幕。希望它有助於編譯錯誤解決方案。

enter image description here

編輯

只是進行一些測試 - 如果我評論類代碼,編譯錯誤消失。

以下class實施中是否有什麼錯誤,我在Swift 4中丟失?

import UIKit 
import AVFoundation 


class CameraController: UIViewController, AVCapturePhotoCaptureDelegate, UIViewControllerTransitioningDelegate { 

    let dismissButton: UIButton = { 
     let button = UIButton(type: .system) 
     button.setImage(#imageLiteral(resourceName: "right_arrow_shadow").withRenderingMode(.alwaysOriginal), for: .normal) 
     button.addTarget(self, action: #selector(handleDismiss), for: .touchUpInside) 
     return button 
    }() 

    let capturePhotoButton: UIButton = { 
     let button = UIButton(type: .system) 
     button.setImage(#imageLiteral(resourceName: "capture_photo").withRenderingMode(.alwaysOriginal), for: .normal) 
     button.addTarget(self, action: #selector(handleCapturePhoto), for: .touchUpInside) 
     return button 
    }() 

    let output = AVCapturePhotoOutput() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     transitioningDelegate = self 
     setupCaptureSession() 
     setupHUD() 
    } 


    // hiddes the status bar 
    override var prefersStatusBarHidden: Bool { return true } 

    //MARK: General funcs 
    fileprivate func setupCaptureSession() { 
     let captureSession = AVCaptureSession() 

     //1. setup inputs - camera 
     let captureDevice = AVCaptureDevice.default(for: AVMediaType.video) 
     do { 
      let input = try AVCaptureDeviceInput(device: captureDevice!) 
      if captureSession.canAddInput(input) { 
       captureSession.addInput(input) 
      } 
     } catch let err { 
      print("Could not setup camera input:", err) 
     } 
     //2. setup outputs 
     if captureSession.canAddOutput(output) { 
      captureSession.addOutput(output) 
     } 
     //3.setup output preview 
     let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)  
     previewLayer.frame = view.frame 
     view.layer.addSublayer(previewLayer) 

     // starts the inputs/outputs 
     captureSession.startRunning() 
    } 


    fileprivate func setupHUD() { 
     // botão de captura 
     view.addSubview(capturePhotoButton) 
     capturePhotoButton.anchor(top: nil, left: nil, bottom: view.bottomAnchor, right: nil, paddingTop: 0, paddinfLeft: 0, paddingBottom: 24, paddingRight: 0, width: 80, height: 80) 
     capturePhotoButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 

     // dismiss button 
     view.addSubview(dismissButton) 
     dismissButton.anchor(top: view.topAnchor, left: nil, bottom: nil, right: view.rightAnchor, paddingTop: 12, paddinfLeft: 0, paddingBottom: 0, paddingRight: 12, width: 50, height: 50) 
    } 


    @objc func handleCapturePhoto() { 
     // processes the captured photo 
     let settings = AVCapturePhotoSettings() 
     guard let preview = settings.availablePreviewPhotoPixelFormatTypes.first else { return } 
     settings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: preview] 
     output.capturePhoto(with: settings, delegate: self) 
    } 


    @objc func handleDismiss() { 
     dismiss(animated: true, completion: nil) 
    } 


    // Camera delegate 
    func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { 
     // access the captured image 
     let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer!, previewPhotoSampleBuffer: previewPhotoSampleBuffer!) 
     let previewImage = UIImage(data: imageData!) 

     // shows the image 
     let containerView = PreviewPhotoContainerView() 
     containerView.previewImageView.image = previewImage 

     view.addSubview(containerView) 
     containerView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddinfLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) 

    } 
} 
+0

你未定義的符號demangles成'爲__ObjC.AVCapturePhotoSettings協議見證表:在AVFoundation' –

+0

AVFoundation._AVCapturePhotoSettingsSwiftNativeTypes @KevinBallard抱歉,但我有點失去了您的評論。 你是說我需要符合AVCapturePhotoSettings協議? –

+1

我很困惑,爲什麼這是另一個問題的重複。在我的iphone7上測試時出現此錯誤。把這個標記爲重複的人喝醉了!我發現了幾種修復我的問題的方法,而不使用該代碼,#if arch(x86_64) –

回答

2

所以我設法解決了這個問題。

不知何故,在Xcode 9中,您不能實現「免費」代碼,這些代碼在iOS模擬器上無法使用。

錯誤 - 解決後 - 非常簡單。

我不能在模擬器中使用相機,因爲它不起作用。

我認爲Xcode可以提供更多有用的錯誤信息。

爲了解決這個問題,在我的CameraController class我不得不編輯我的handleCapturePhoto()函數。

我必須檢查,如果使用的架構不是x86_64(模擬器),那麼我可以呈現相機輸出。

這是函數解決狀態:

@objc func handleCapturePhoto() { 
    // processes the captured photo 
    let settings = AVCapturePhotoSettings() 

    // doesn't show in the simulator 
    #if (!arch(x86_64)) 
     guard let preview = settings.previewPhotoFormat?.first else { return } 
     settings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: preview] 
     output.capturePhoto(with: settings, delegate: self) 
    #endif 
} 

希望這可以幫助別人的未來。

+0

這會讓您構建並運行您的項目,但不是解決問題的真正解決方案。我試過這個,並且替換了我找到的其他解決方案,而沒有使用'#if(!arch(x86_64))',而且它的行爲並不像預期的那樣。但是,如果它適合你,它適用於你。請注意,此解決方案可能會解決該問題,但會替換爲新問題 –

+0

這非常簡單。如果設備是模擬器 - 請勿打開相機。這只是爲了測試,在發佈版本中,我可以刪除#ifdef反正如果我想 –

+0

正如我所提到的,我在我的iPhone 7上試過這個解決方案,並且它刪除了編譯錯誤,但它的行爲方式與預期。所以,請確保您的所有功能都按預期工作。如果是這樣,你很好。 –