0

我进行了一些搜索,大多数答案要么在 Obj-C 中,要么不适用于此代码。

我正在使用本指南:http ://drivecurrent.com/devops/using-swift-and-avfoundation-to-create-a-custom-camera-view-for-an-ios-app/

我从中得到了这段代码:

     super.viewWillAppear(animated)

    captureSession = AVCaptureSession()
    captureSession!.sessionPreset = AVCaptureSessionPresetPhoto

    let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    let frontCamera = ????

    var error: NSError?
    var input: AVCaptureDeviceInput!
    do {
        input = try AVCaptureDeviceInput(device: backCamera)
    } catch let error1 as NSError {
        error = error1
        input = nil
    }

后置摄像头是默认设置,但我没有看到如何直接访问前置摄像头以让人们来回交换。

4

2 回答 2

3

您当前无效的frontCamera与您的backCamera具有非常相似的行为。您需要使用它允许将所有能够捕获 AVMediaTypeVideo 类型数据的设备存储在一个数组中。devicesWithMediaType(_ mediaType: String!)

let captureDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
var frontCamera: AVCaptureDevice

for device in captureDevices {
    let device = element as! AVCaptureDevice
    if device.position == AVCaptureDevicePosition.Front {           
        frontCamera = device
        break
    }
}
于 2016-06-17T19:31:25.497 回答
0
func FromCamera() {
    let imagepicker = UIImagePickerController()
    imagepicker.delegate = self
    imagepicker.allowsEditing = true
    imagepicker.sourceType = UIImagePickerController.isSourceTypeAvailable(.Camera) ? .Camera : .PhotoLibrary
    imagepicker.cameraDevice = UIImagePickerController.isCameraDeviceAvailable(.Front) ? .Front : .Rear
    self.presentViewController(imagepicker, animated: true, completion: { _ in })
}
于 2017-01-27T05:11:48.100 回答