更新到 Xcode 9 / Swift 4 后 AVCapture 出现奇怪的编译错误
Weird compile error with AVCapture after updating to Xcode 9 / Swift 4
我有一个使用 AVCapturePhoto
的普通 UIViewController
,我在其中创建了一个自定义捕获控制器。
一切正常,直到我更新到 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 打印屏幕。希望对解决编译错误有帮助
编辑
只是执行一些测试——如果我注释 class 代码,编译错误就会消失。
我在 Swift 4
中遗漏的以下 class
实现有什么问题吗??
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)
}
}
所以我设法解决了这个问题。
不知何故,使用 Xcode 9 你无法实现 "free" 无法在 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
}
希望这对以后的人有所帮助。
我有一个使用 AVCapturePhoto
的普通 UIViewController
,我在其中创建了一个自定义捕获控制器。
一切正常,直到我更新到 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 打印屏幕。希望对解决编译错误有帮助
编辑
只是执行一些测试——如果我注释 class 代码,编译错误就会消失。
我在 Swift 4
中遗漏的以下 class
实现有什么问题吗??
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)
}
}
所以我设法解决了这个问题。
不知何故,使用 Xcode 9 你无法实现 "free" 无法在 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
}
希望这对以后的人有所帮助。