我使用Xcode 10.0和Swift 4.2,我想制作11张具有不同曝光时间的图片。
例如,第一张照片是基本的,但是在我想将曝光时间除以2后,如:last_exposure_time / 2,当我在第5张照片中时,我采用原始的曝光时间,然后乘以2,这样我就得到了原始图片,将5次曝光除以2,将5次曝光乘以2。
实际上我有以下代码:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var captureSession = AVCaptureSession()
var backCamera: AVCaptureDevice?
var frontCamera: AVCaptureDevice?
var currentCamera: AVCaptureDevice?
var Arrayimages: Array<UIImage> = []
var photoOutput: AVCapturePhotoOutput?
var cameraPreview: AVCaptureVideoPreviewLayer?
var image: UIImage?
var lastExifExposureTime: CMTime?
var pictureNumber: Int = 0
var imageView: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
setupCaptureSession()
setupDevice()
setupInputOutput()
setupPreviewLayer()
startRunningCaptureSession()
}
func setupCaptureSession(){
captureSession.sessionPreset = AVCaptureSession.Preset.photo
}
func setupDevice(){
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
let devices = deviceDiscoverySession.devices
for device in devices {
if device.position == AVCaptureDevice.Position.back{
backCamera = device
}else if device.position == AVCaptureDevice.Position.front{
frontCamera = device
}
}
currentCamera = backCamera
}
func setupInputOutput(){
do{
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
captureSession.addInput(captureDeviceInput)
photoOutput = AVCapturePhotoOutput()
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
if captureSession.canAddOutput(photoOutput!) {
captureSession.addOutput(photoOutput!)
}
}catch{
print(error)
}
}
func setupPreviewLayer(){
cameraPreview = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreview?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreview?.connection?.videoOrientation = AVCaptureVideoOrientation.landscapeRight
cameraPreview?.frame = self.view.frame
self.view.layer.insertSublayer(cameraPreview!, at: 0)
}
func startRunningCaptureSession(){
captureSession.startRunning()
}
func stoptRunningCaptureSession(){
captureSession.stopRunning()
}
@IBAction func cameraButton_TouchUpInside(_ sender: Any) {
do{
try currentCamera?.lockForConfiguration()
}catch{
print(error)
}
photoOutput?.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
}
}
extension ViewController: AVCapturePhotoCaptureDelegate{
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if pictureNumber > 11 {
print("finish")
}
else{
if let imageData = photo.fileDataRepresentation(){
image = UIImage(data: imageData)
imageView?.image = image
Arrayimages.append(image!)
print(Arrayimages[pictureNumber])
UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
//imageView!.image = image
let newDurationSeconds = Float64(CMTimeGetSeconds(currentCamera!.exposureDuration))
let timeNanoSeconds = newDurationSeconds * 1000000000.0
print(newDurationSeconds)
let newExposureTime = CMTimeMakeWithSeconds(Float64(newDurationSeconds),preferredTimescale: Int32(timeNanoSeconds) / 3 )
currentCamera?.setExposureModeCustom(duration: newExposureTime, iso: 100, completionHandler: nil)
photoOutput?.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
pictureNumber += 1
}
}
}
}
实际上我得到了我所有的照片,但是只有秒针被修改了,并且曝光时间没有改变。
如何制作原始图片,将曝光时间除以2的5和将曝光时间乘以2的5?
谢谢!!