简体   繁体   English

黄色警告:从“AVCaptureVideoPreviewLayer”到“AVCaptureVideoPreviewLayer”的条件转换总是成功

[英]Yellow warning: Conditional cast from 'AVCaptureVideoPreviewLayer' to 'AVCaptureVideoPreviewLayer' always succeeds

I have this bit of code and it's throwing a yellow warning.我有这段代码,它发出黄色警告。 I can't work out how to code it so the yellow warning goes away.我不知道如何编码,所以黄色警告消失了。 Trying to clean up my code after converting from Swift-2 -> 3 -> 4.从 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)    
}

Removing as? AVCaptureVideoPreviewLayer删除as? AVCaptureVideoPreviewLayer as? AVCaptureVideoPreviewLayer shows an error: as? AVCaptureVideoPreviewLayer显示错误:

Initializer for conditional binding must have Optional type, not 'AVCaptureVideoPreviewLayer'条件绑定的初始化程序必须具有 Optional 类型,而不是“AVCaptureVideoPreviewLayer”

AVCaptureVideoPreviewLayer(session: captureSession) always returns an object of type AVCaptureVideoPreviewLayer , not of type AVCaptureVideoPreviewLayer? AVCaptureVideoPreviewLayer(session: captureSession)总是返回一个AVCaptureVideoPreviewLayer类型的对象,而不是AVCaptureVideoPreviewLayer?类型的对象AVCaptureVideoPreviewLayer? (ie the returned object is not an optional and so it can't be nil). (即返回的对象不是可选的,因此它不能为零)。

When you do if let , Swift expects an Optional type.当你执行if let ,Swift 需要一个 Optional 类型。 Since no optional type is created, the error occurs.由于没有创建可选类型,因此发生错误。

My suggestion is to remove the if part and just have我的建议是删除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) 

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 黄色警告:从UITextDocumentProxy到UIKeyInput的条件强制转换始终成功 - yellow Warnings : Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds 如何删除警告“从'Any'到'AnyObject'的有条件强制转换始终成功” - How to remove warning “Conditional cast from 'Any' to 'AnyObject' always succeeds” 从 String 到 NSString 的条件转换总是成功 - Conditional cast from String to NSString always succeeds 从“UIButton”到“UIButton”的条件转换总是成功 - Conditional cast from 'UIButton' to 'UIButton' always succeeds 从 'AFError' 到 'AFError' 的条件转换总是成功 - Conditional cast from 'AFError' to 'AFError' always succeeds 从AVCaptureVideoPreviewLayer实时缩放 - Realtime zoom from AVCaptureVideoPreviewLayer 从“AppDelegate”到“UNUserNotificationCenterDelegate”的条件转换总是成功(颤振) - Conditional cast from 'AppDelegate' to 'UNUserNotificationCenterDelegate' always succeeds (Flutter) AVCaptureVideoPreviewLayer 和从相机位置预览 - AVCaptureVideoPreviewLayer and preview from camera position AVCaptureVideoPreviewLayer 的比例 - Scale of AVCaptureVideoPreviewLayer 防止带有AVCaptureVideoPreviewLayer的UIView与其他ViewController一起旋转 - Prevent UIView with AVCaptureVideoPreviewLayer from rotating with rest of ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM