【问题标题】:Swift 3 and AVCaptureDevice depreciation, when trying to find Camera nameSwift 3 和 AVCaptureDevice 折旧,尝试查找相机名称时
【发布时间】:2017-03-04 16:37:23
【问题描述】:

我遇到了这个问题,我在 iOS9 中编写 Swift 3 代码,因为它与我预期的大多数有用设备现在一样古老。不是每个人都升级了——或者可以升级到 iOS10。我的 iPad Mini 无法升级。 我需要获取相机的名称,并且在我的 iPhone 上的 iOS 10 中运行时遇到了贬值的代码。 我在 Objective-C 中找到了答案,并通过 Swiftify 运行了它。快。

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")

【问题讨论】:

    标签: ios swift3 ios-camera


    【解决方案1】:

    好的,这是获得两者的答案 运行在ios9和10以上的代码,避免设备位贬值, 并返回内置设备的名称。

    这是在 10 折旧。

    AVCaptureDevice.devices()

    所以编辑建议我添加你在这里看到的代码开始。

    如果#available ....

    并打印我在任何地方都找不到的设备本地化名称。所以我找到了这个,并用Swiftify将它改成了Swift 3

    NSLog(@"设备名称:%@", [设备本地化名称]);

    显示的代码会产生这个

    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()
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 2017-02-18
      • 2022-12-06
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多