2017-03-04 84 views
0

我遇到了這個問題,我在哪裏編碼Swift 3在iOS9,因爲這大約是我認爲現在最有用的設備的年齡。不是每個人都升級 - 或者可以升級到iOS10。我的iPad Mini無法升級。 我需要獲取相機的名稱,並在iPhone上運行iOS 10時遇到折舊代碼。 我在Objective-C中找到答案,並通過Swiftify運行它。普雷斯托。Swift 3和AVCaptureDevice折舊,當試圖找到攝像頭名稱

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

回答

1

好吧,這就是答案,這是讓兩個 上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() 
        } 
       } 
      } 
     } 
    } 
} 
相關問題