问题描述
我试图将Camera View放入我的View Controller中.
I am trying to simply put a Camera View in my View Controller.
我在顶部导入了AVFoundation
以及UIImagePickerControllerDelegate
和UINavigationControllerDelegate
类.
I imported AVFoundation
at the top, as well as UIImagePickerControllerDelegate
and UINavigationControllerDelegate
classes.
但是,每当我尝试使用AVCaptureStillImageOutput
时,Xcode都会告诉我它已在iOS10中弃用,我应该使用AVCapturePhotoOutput
.完全可以,但是,一旦我想调用stillImageOutput.outputSettings
,.outputSettings
本身就不可用.因此,我必须使用AVAVCaptureStillImageOutput
使其起作用,但是我有多个警告,因为此功能在iOS10中已弃用.
However, whenever I try to use AVCaptureStillImageOutput
, Xcode tells me that it was deprecated in iOS10 and I should use AVCapturePhotoOutput
. That is completely fine, however, as soon as I want to call stillImageOutput.outputSettings
, .outputSettings
itself is not available. Thus, I have to use AVAVCaptureStillImageOutput
for it to work but I have multiple warnings because this function was deprecated in iOS10.
我进行了搜索,但找不到真正的解决方案.我将衷心感谢您的帮助.我正在学习,所以任何解释都很好!代码在下面.
I searched and searched but could not really find the solution around it. I would really appreciate your help. I am learning so any explanation would be great! Code is below.
import UIKit
import AVFoundation
class CameraView: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var captureSession : AVCaptureSession?
var stillImageOutput : AVCaptureStillImageOutput?
var previewLayer : AVCaptureVideoPreviewLayer?
@IBOutlet var cameraView: UIView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
captureSession = AVCaptureSession()
captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080
var backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var error : NSError?
do {
var input = try! AVCaptureDeviceInput (device: backCamera)
if (error == nil && captureSession?.canAddInput(input) != nil) {
captureSession?.addInput(input)
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if (captureSession?.canAddOutput(stillImageOutput) != nil) {
captureSession?.addOutput(stillImageOutput)
previewLayer = AVCaptureVideoPreviewLayer (session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
cameraView.layer.addSublayer(previewLayer!)
captureSession?.startRunning()
}
}
} catch {
}
}
}
推荐答案
AVCaptureStillImageOutput
被弃用意味着您可以在iOS 10中继续使用它,但是:
AVCaptureStillImageOutput
being deprecated means you can keep using it in iOS 10, but:
- Apple无法保证iOS 10可以使用多长时间.
- 随着iOS 10及更高版本中增加了新的硬件和软件功能,您将无法访问所有这些功能.例如,您可以 为宽色设置
AVCaptureStillImageOutput
,但是使用AVCapturePhotoOutput
进行宽色设置要容易得多.对于RAW捕获或实时照片,AVCapturePhotoOutput
是镇上唯一的游戏.
- Apple makes no promises as to how long past iOS 10 it'll stay available.
- as new hardware and software features get added in iOS 10 and beyond, you won't get access to all of them. For example, you can set up
AVCaptureStillImageOutput
for wide color but it's a lot easier to do wide color withAVCapturePhotoOutput
. And for RAW capture or Live Photos,AVCapturePhotoOutput
is the only game in town.
如果您尽管不赞成而仍乐于继续,则问题不是不是outputSettings
已删除— 它仍然存在.
If you're happy proceeding despite the deprecation, your issue isn't that outputSettings
is removed — it's still there.
对于beta 6及更高版本需要注意的一些事情(尽管事实证明这里不是问题):使用NSDictionary
且没有显式键和值类型的API会以[AnyHashable: Any]
的形式出现在Swift 3中,而Foundation或您可能在字典中使用的CoreFoundation类型不再隐式地桥接到Swift类型. (某些有关beta 6词典转换的其他问题可能会为您指出正确的方向.)
Something to be aware of for beta 6 and beyond (though it turns out not to be an issue here): APIs that use NSDictionary
without explicit key and value types come into Swift 3 as [AnyHashable: Any]
and the Foundation or CoreFoundation types you might use in a dictionary are no longer implicitly bridged to Swift types. (Some of the other questions about beta 6 dictionary conversions might point you in the right direction there.)
但是,设置outputSettings
并没有出现任何编译错误.无论是使用完整的代码,还是将其简化为该行的基本部分:
However, I'm not getting any compilation errors for setting outputSettings
. Whether in your full code or by reducing it to the essential parts for that line:
var stillImageOutput : AVCaptureStillImageOutput?
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
...我看到的唯一警告是关于弃用.
...the only warnings I see are about the deprecation.
这篇关于Swift 3中的AVCaptureStillImageOutput与AVCapturePhotoOutput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!