1

我正在尝试编写一个基本的 iOS 应用程序,该应用程序可以使用一些固定设置捕获 RAW 照片。但是我在拍照时遇到了问题。我在设备配置中选择的设置(iPhone Xs 的摄像头)显示在预览中(固定焦点,改变照明,取决于我在代码中选择的 iso),当我按下捕获按钮和运行 capturePhoto 方法。然后我可以看到调焦和手电筒点火。一开始我怀疑会话预设会覆盖我的设置,一旦我开始会话,但锁定设备直到会话运行没有帮助。提前致谢!

我的代码:

import UIKit
import AVFoundation
import Photos

class ViewController: UIViewController {

    //instance variables:

    //dispatch-queue for session setup:
    var setupQueue  = DispatchQueue(label: "sessionSetupQueue")

    //session-related:
    var captureSession:     AVCaptureSession!
    var camera:             AVCaptureDevice!
    var captureInput:       AVCaptureInput!
    var photoOutput:        AVCapturePhotoOutput!
    var photoSettings:      AVCapturePhotoSettings!

    //view-model:
    var previewLayer:       AVCaptureVideoPreviewLayer!

    @IBOutlet weak var capturePreviewView: UIView!
    @IBOutlet weak var photoCaptureButton: UIButton!

}

//extension with methods and functions:

extension ViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        do {
            try self.configureSession()

                //Create a preview view:
            self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
            self.previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

            //connect preview view to UIView object


            self.capturePreviewView.layer.insertSublayer(self.previewLayer, at: 0)
            self.previewLayer.frame = self.view.frame


        } catch {
            print("could not set up a CaptureSession!")
            return
        }
    }

    //configuration of the capture session
    func configureSession() throws {

        // create a capture session and set it up for configuration
        self.captureSession = AVCaptureSession()
        captureSession.sessionPreset = .photo
        captureSession.beginConfiguration()

        //find tele-camera and set it as input to the session
        let camera = AVCaptureDevice.default(.builtInTelephotoCamera, for: .video, position: .back)
        self.camera = camera

        //configure the device before adding it as an input to the session
        do {
            try configureDevice()
        }
        guard let captureInput = try? AVCaptureDeviceInput(device: self.camera!), captureSession.canAddInput(captureInput) else { return }
        captureSession.addInput(captureInput)

        //Set an output for the session:
        self.photoOutput = AVCapturePhotoOutput()
        photoOutput.isHighResolutionCaptureEnabled = true
        guard captureSession.canAddOutput(photoOutput) else { return }
        captureSession.addOutput(photoOutput)

        //commit the configuration:
        captureSession.commitConfiguration()

        //start the session:
        captureSession.startRunning()

    }

    //configure the device

    func configureDevice() throws {

        //lock the device for setup

        do {
            try camera.lockForConfiguration()
        } catch {
            print("could not lock device!")
            return
        }

        //set camera parameters
        camera.focusMode    = .locked
        if camera.isLockingFocusWithCustomLensPositionSupported {
            camera.setFocusModeLocked(lensPosition: 1.0, completionHandler: nil)
        }
        if camera.isExposureModeSupported(.custom) {
            camera.setExposureModeCustom(duration: AVCaptureDevice.currentExposureDuration, iso: 60, completionHandler: nil)
        }
        camera.torchMode    = .off

        //end setup by unlocking for configuration
        camera.unlockForConfiguration()

    }

    //capture fuction

    @IBAction func capturePhoto (_ sender: UIButton) {

        //settings configuration

        //setup settings for RAW capture
        self.photoSettings  = AVCapturePhotoSettings(rawPixelFormatType: self.photoOutput.availableRawPhotoPixelFormatTypes.first!)

        //disable red eye correction, image stabilization and flash
        photoSettings.isAutoRedEyeReductionEnabled = false
        photoSettings.isAutoStillImageStabilizationEnabled = false
        photoSettings.flashMode = .on

        //capture the photo
        self.photoOutput.capturePhoto(with: self.photoSettings, delegate: self)

        //visual effects:

        self.capturePreviewView.layer.opacity = 0
        UIView.animate(withDuration: 0.25) {
            self.capturePreviewView.layer.opacity = 1
        }

    }

}

//photoOutput delegate functions:

extension ViewController: AVCapturePhotoCaptureDelegate {

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        if error != nil { print("Error capturing photo!"); return }

        PHPhotoLibrary.requestAuthorization { status in
            guard status == .authorized else { return }
            PHPhotoLibrary.shared().performChanges({
                //add captured photos data to asset
                let creationRequest =  PHAssetCreationRequest.forAsset()
                creationRequest.addResource(with: .photo, data: photo.fileDataRepresentation()!, options: nil)
            })
        }

    }

}
4

1 回答 1

1

我找到了解决方案:

问题是 photoSettings 中的 flashMode。使用 flashMode = .on 会覆盖我事先选择的大部分设备设置。

为了评估场景的闪光强度,该设备使用了一些自动对焦和手电筒。

没有闪光灯,自定义曝光设置正确,焦点保持固定。

于 2018-11-08T12:18:14.417 回答