我正在尝试编写一个基本的iOS应用程序,用一些固定的设置捕捉原始照片。但是我在拍照的时候遇到了一个问题。我在设备配置中选择的设置( iPhone x的远程版本)显示在预览中(固定焦点,根据代码中选择的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)
})
}
}
}发布于 2018-11-08 12:18:14
我找到了解决办法:
问题在于photoSettings中的photoSettings。使用flashMode = .on覆盖我预先选择的大多数设备设置。
为了评估场景的闪光强度,该装置使用了一些自动对焦和火炬。
没有闪光灯,定制曝光设置正确,焦点保持固定。
https://stackoverflow.com/questions/53203169
复制相似问题