gpt4 book ai didi

swift - 黄色警告 : Conditional cast from 'AVCaptureVideoPreviewLayer' to 'AVCaptureVideoPreviewLayer' always succeeds

转载 作者:行者123 更新时间:2023-11-28 05:53:24 27 4
gpt4 key购买 nike

我有这段代码,它发出黄色警告。我不知道如何编码,所以黄色警告消失了。从 Swift-2 -> 3 -> 4 转换后尝试清理我的代码。

if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) as? AVCaptureVideoPreviewLayer {
previewLayer.bounds = imageView.bounds
previewLayer.position = CGPoint(x: 140, y: 140)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
imageView.layer.addSublayer(previewLayer)
view.addSubview(imageView)
}

删除为? AVCaptureVideoPreviewLayer 显示错误:

Initializer for conditional binding must have Optional type, not 'AVCaptureVideoPreviewLayer'

最佳答案

AVCaptureVideoPreviewLayer(session: captureSession) 总是返回 AVCaptureVideoPreviewLayer 类型的对象,而不是 AVCaptureVideoPreviewLayer? 类型的对象(即返回的对象不是一个可选的,所以它不能为零)。

当你执行 if let 时,Swift 需要一个 Optional 类型。由于没有创建可选类型,因此发生错误。

我的建议是删除 if 部分,只包含

let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.bounds = imageView.bounds
previewLayer.position = CGPoint(x: 140, y: 140)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
imageView.layer.addSublayer(previewLayer)
view.addSubview(imageView)

希望这对您有所帮助!

关于swift - 黄色警告 : Conditional cast from 'AVCaptureVideoPreviewLayer' to 'AVCaptureVideoPreviewLayer' always succeeds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52130429/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com