我使用各种主题和灯光进行了多项测试.每个测试都显示标准iOS相机应用程序的质量显着优于(颜色没有褪色,更好的聚焦,更好的照明,更少的颗粒感)到我的自定义基于AVFoundation的应用程序.我无法解释巨大的差异.下面是使用这两种方法拍摄的视频的屏幕截图(使用前置摄像头).
自定义实现代码:
let chosenCameraType = AVCaptureDevicePosition.Front
//get camera
let devices = AVCaptureDevice.devices()
for device in devices
{
if (!device.hasMediaType(AVMediaTypeVideo))
{
continue
}
if (device.position != chosenCameraType)
{
continue
}
camera = (device as? AVCaptureDevice)!
}
do
{
captureSession = AVCaptureSession()
captureSession!.sessionPreset = AVCaptureSessionPresetHigh
let video = try AVCaptureDeviceInput(device: camera) as AVCaptureDeviceInput
captureSession!.addInput(video)
let audio = try AVCaptureDeviceInput(device: AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)) as AVCaptureDeviceInput
captureSession!.addInput(audio)
fileOutput = AVCaptureMovieFileOutput()
captureSession?.addOutput(fileOutput)
captureSession!.startRunning()
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let name = String(UInt64(NSDate().timeIntervalSince1970 * 1000))
fileOutput?.startRecordingToOutputFileURL(NSURL(fileURLWithPath: "\(documentsPath)/" + name + ".mov"), recordingDelegate: self)
}
catch let error as NSError
{
print(error)
}
请试试!你也会看到差异……
最佳答案 我已经注意到你所描述的内容并查看了我看不到你实现的代码:
backCamera.focusPointOfInterest = focusPoint
backCamera.focusMode = AVCaptureFocusMode.autoFocus
backCamera.exposureMode = AVCaptureExposureMode.autoExpose
我在touchesBegan中实现了这个,这样相机就可以专注于用户触摸屏幕的区域.这是代码部分:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touchPoint = touches.first
let x = (touchPoint?.location(in: self.view).x)! / self.view.bounds.width
let y = (touchPoint?.location(in: self.view).y)! / self.view.bounds.height
let realX = (touchPoint?.location(in: self.view).x)!
let realY = (touchPoint?.location(in: self.view).y)!
let focusPoint = CGPoint(x: x, y: y)
let k = DrawSquare(frame: CGRect(
origin: CGPoint(x: realX - 75, y: realY - 75),
size: CGSize(width: 150, height: 150)))
if backCamera != nil {
do {
try backCamera.lockForConfiguration()
self.previewView.addSubview(k)
}
catch {
print("Can't lock back camera for configuration")
}
if backCamera.isFocusPointOfInterestSupported {
backCamera.focusPointOfInterest = focusPoint
}
if backCamera.isFocusModeSupported(AVCaptureDevice.FocusMode.autoFocus) {
backCamera.focusMode = AVCaptureDevice.FocusMode.autoFocus
}
if backCamera.isExposureModeSupported(AVCaptureDevice.ExposureMode.autoExpose) {
backCamera.exposureMode = AVCaptureDevice.ExposureMode.autoExpose
}
backCamera.unlockForConfiguration()
}
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
k.removeFromSuperview()
}
}
当相机显示暗图像时,用户可以简单地点击屏幕,它将调整曝光和聚焦,从而使图像变亮.