我正在尝试编写一个基本的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)
})
}
}
}
答案 0 :(得分:0)
我找到了解决方法:
问题出在photoSettings中的flashMode。 使用flashMode = .on会覆盖我之前选择的大多数设备设置。
为了评估场景的闪光强度,设备使用了一些自动对焦和手电筒。
没有闪光灯,自定义曝光设置正确,焦点保持固定。