Ios Swift使用与拍照相同的方法录制视频
我目前正在使用以下代码允许用户拍照:Ios Swift使用与拍照相同的方法录制视频,ios,swift,avfoundation,Ios,Swift,Avfoundation,我目前正在使用以下代码允许用户拍照: private func configurePhotoView() { capturedPhoto.contentMode = .ScaleAspectFill capturedPhoto.clipsToBounds = true capturedPhoto.hidden = true captureSession = AVCaptureSession() captureSession!.sessionPreset
private func configurePhotoView() {
capturedPhoto.contentMode = .ScaleAspectFill
capturedPhoto.clipsToBounds = true
capturedPhoto.hidden = true
captureSession = AVCaptureSession()
captureSession!.sessionPreset = AVCaptureSessionPresetPhoto
photoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var error: NSError?
photoDeviceInput = AVCaptureDeviceInput(device: photoCaptureDevice, error: &error)
if error == nil && captureSession!.canAddInput(photoDeviceInput) {
captureSession!.addInput(photoDeviceInput)
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if captureSession!.canAddOutput(stillImageOutput) {
captureSession!.addOutput(stillImageOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
cameraView.layer.addSublayer(previewLayer)
captureSession!.startRunning()
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "focusPhoto:"))
}
}
else {
//TODO: Handle error
}
photoOverlay.backgroundColor = UIColor(white: 0, alpha: 0.5)
}
当他们按下按钮时,调用此函数:
@IBAction func didPressTakePhoto(sender: UIButton) {
if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
if (sampleBuffer != nil) {
var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
var dataProvider = CGDataProviderCreateWithCFData(imageData)
var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)
self.capturedImage = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right)
self.capturedPhoto.image = self.capturedImage
}
})
}
}
这段代码允许我用我想要的任何纵横比拍照。有没有办法修改didPressTakePhoto
代码来制作视频
我甚至找不到关于如何制作定制录像机的swift教程。您的
AVCaptureSession
当前只有一个输出,即AVCaptureStillImageOutput
。如果要捕获视频,需要将AVCaptureVideoDataOutput
或AVCaptureMovieFileOutput
添加为捕获会话的输出
AV基金会编程指南:。Hi@leekaiinthesky你知道如何修复视频开头和结尾的模糊吗?问题:。谢谢你的时间!