我用这个来捕捉图像
var captureDevice : AVCaptureDevice?
var picker = UIImagePickerController()
var captureSession = AVCaptureSession()
var stillImageOutput = AVCaptureStillImageOutput()
var captureVideoPreviewLayer = AVCaptureVideoPreviewLayer()
func captureImage (sender:AnyObject?) {
isCapturingImage = true
var videoConnection :AVCaptureConnection? = nil
for connection:AVCaptureConnection in stillImageOutput.connections as! [AVCaptureConnection] {
for port:AVCaptureInputPort in connection.inputPorts as! [AVCaptureInputPort]{
if(port.mediaType == AVMediaTypeVideo){
videoConnection = connection
break;
}
}
if((videoConnection) != nil) {break}
}
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
(imageSampleBuffer:CMSampleBufferRef! , error :NSError!) in
if (imageSampleBuffer != nil) {
var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
let capturedImage = UIImage.init(data: imageData, scale:1)
}
})
}
如何实现捕获视频?我几乎找不到任何关于自定义视频捕获的教程。任何人都可以实现捕捉视频的功能吗?
答案 0 :(得分:1)
使用以下代码使用AVFoundation
class ViewController: UIViewController,AVCaptureFileOutputRecordingDelegate {
@IBOutlet weak var cameraView: UIView!
var captureSession : AVCaptureSession!
var movieFileOutput : AVCaptureMovieFileOutput!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
beginSession()
}
func beginSession() {
captureSession = AVCaptureSession()
do {
captureSession.beginConfiguration()
let cameraDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) as AVCaptureDevice
let cameraDeviceInput = try AVCaptureDeviceInput(device: cameraDevice)
if (captureSession.canAddInput(cameraDeviceInput) == true) {
captureSession.addInput(cameraDeviceInput)
}
let audioDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio) as AVCaptureDevice
let audioDeviceInput = try AVCaptureDeviceInput(device: audioDevice)
if (captureSession.canAddInput(audioDeviceInput) == true) {
captureSession.addInput(audioDeviceInput)
}
movieFileOutput = AVCaptureMovieFileOutput()
if (captureSession.canAddOutput(movieFileOutput) == true) {
captureSession.addOutput(movieFileOutput)
}
captureSession.commitConfiguration()
captureSession.startRunning()
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
previewLayer.position = CGPoint(x: CGRectGetMidX(self.view.bounds), y: CGRectGetMidY(self.view.bounds))
previewLayer.videoGravity = AVLayerVideoGravityResize
cameraView.layer.addSublayer(previewLayer)
}
catch let error as NSError {
print("\(error), \(error.localizedDescription)")
}
}
func startRecording() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as NSString
let outputFile = documentsDirectory.stringByAppendingPathComponent("output.mov")
let outputURL = NSURL(fileURLWithPath:outputFile)
movieFileOutput.startRecordingToOutputFileURL(outputURL, recordingDelegate: self)
}
func stopRecording() {
movieFileOutput.stopRecording()
captureSession.stopRunning()
}
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
print("Video URL =\(outputFileURL)")
}
}
停止播放视频后AVFoundation
调用didFinishRecordingToOutputFileAtURL
代理,其中包含参数outputFileURL
,这是录制视频的路径。
希望这会有所帮助。