好吧,這就是答案,這是讓兩個 上ios9運行的代碼,和10後,避免了設備位的折舊, 並返回內置的設備的名稱。
這10
AVCaptureDevice.devices()折舊
所以編輯建議我加你在這裏看到開頭的代碼。
如果#available ....
和打印設備本地化的名稱我不能」找不到任何相關文檔。所以我找到了這個,並將其更改爲Swift 3 Swiftify
NSLog(@「Device name:%@」,[device localizedName]);
所示的代碼產生這種
Device name: Optional("Back Camera")
Device name: Optional("Back Camera")
Capture device found
Capture session running
Device name: Optional("Front Camera")
Device name: Optional("iPhone Microphone")
很抱歉,如果這是令人困惑 - 但下面的代碼是非常簡單的。希望能幫助到你。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//=======================
captureSession.sessionPreset = AVCaptureSessionPresetHigh
if #available(iOS 10.0, *) {
if let devices = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .back) {
print("Device name: \(devices.localizedName)")
}
} else {
// Fallback on earlier versions
}
if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] {
// Loop through all the capture devices on this phone
for device in devices {
print("Device name: \(device.localizedName)")
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera
if(device.position == AVCaptureDevicePosition.back) {
captureDevice = device
if captureDevice != nil {
print("Capture device found")
beginSession()
}
}
}
}
}
}