Hello, here is my example. I am testing on an iPhone 14 pro on ios 16.3. I still cannot get it to stop autofocusing.
import UIKit
import AVFoundation
class ViewController: UIViewController {
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
var photoOutput: AVCapturePhotoOutput!
override func viewDidLoad() {
super.viewDidLoad()
setupCamera()
setupCaptureButton()
}
func setupCamera() {
captureSession = AVCaptureSession()
captureSession.sessionPreset = .photo
guard let device = AVCaptureDevice.default(for: .video) else { return }
do {
let input = try AVCaptureDeviceInput(device: device)
if captureSession.canAddInput(input) {
captureSession.addInput(input)
}
try device.lockForConfiguration()
device.isSubjectAreaChangeMonitoringEnabled = false
device.exposureMode = .locked
device.setFocusModeLocked(lensPosition: 0.1);
device.unlockForConfiguration()
photoOutput = AVCapturePhotoOutput()
if captureSession.canAddOutput(photoOutput) {
captureSession.addOutput(photoOutput)
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)
DispatchQueue.global(qos: .default).async {
self.captureSession.startRunning()
}
} catch {
print("Error setting up camera: \(error)")
}
}
@objc func takePhoto() {
let rawPixelFormats=photoOutput.availableRawPhotoPixelFormatTypes;
let settings = AVCapturePhotoSettings(rawPixelFormatType: rawPixelFormats[0]);
settings.flashMode = .on
settings.isAutoRedEyeReductionEnabled = false;
settings.isAutoVirtualDeviceFusionEnabled = false;
settings.isPortraitEffectsMatteDeliveryEnabled=false;
settings.isAutoContentAwareDistortionCorrectionEnabled=false;
settings.isDepthDataDeliveryEnabled=false;
settings.embedsDepthDataInPhoto=false;
settings.isDepthDataFiltered = false;
settings.embedsSemanticSegmentationMattesInPhoto = false;
settings.embedsPortraitEffectsMatteInPhoto = false;
photoOutput.capturePhoto(with: settings, delegate: self)
}
func setupCaptureButton() {
let captureButton = UIButton(frame: CGRect(x: view.center.x - 50, y: view.bounds.maxY - 120, width: 100, height: 100))
captureButton.backgroundColor = .white
captureButton.layer.cornerRadius = 50
captureButton.addTarget(self, action: #selector(takePhoto), for: .touchUpInside)
view.addSubview(captureButton)
}
}
extension ViewController: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let error = error {
print("Error capturing photo: \(error)")
} else {
if let imageData = photo.fileDataRepresentation(), let image = UIImage(data: imageData) {
//UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}
}
}
Topic:
Media Technologies
SubTopic:
Photos & Camera
Tags: