我正在使用自定义相机应用程序,本教程使用的是iOS 10弃用的AVCaptureStillImageOutput。我已经设置了相机,现在只能使用如何拍照了。
这是我有相机的全景
import UIKit import AVFoundation var cameraPos = "back" class View3: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate { @IBOutlet weak var clickButton: UIButton! @IBOutlet var cameraView: UIView! var session: AVCaptureSession? var stillImageOutput: AVCapturePhotoOutput? var videoPreviewLayer: AVCaptureVideoPreviewLayer? override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) clickButton.center.x = cameraView.bounds.width/2 loadCamera() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) } @IBAction func clickCapture(_ sender: UIButton) { if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) { // This is where I need help } } @IBAction func changeDevice(_ sender: UIButton) { if cameraPos == "back" {cameraPos = "front"} else {cameraPos = "back"} loadCamera() } func loadCamera() { session?.stopRunning() videoPreviewLayer?.removeFromSuperlayer() session = AVCaptureSession() session!.sessionPreset = AVCaptureSessionPresetPhoto var backCamera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front) if cameraPos == "back" { backCamera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .back) } var error: NSError? var input: AVCaptureDeviceInput! do { input = try AVCaptureDeviceInput(device: backCamera) } catch let error1 as NSError { error = error1 input = nil print(error!.localizedDescription) } if error == nil && session!.canAddInput(input) { session!.addInput(input) stillImageOutput = AVCapturePhotoOutput() if session!.canAddOutput(stillImageOutput) { session!.addOutput(stillImageOutput) videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session) videoPreviewLayer?.frame = cameraView.bounds videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill videoPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait cameraView.layer.addSublayer(videoPreviewLayer!) session!.startRunning() } } } }
这是我需要帮助的地方
@IBAction func clickCapture(_ sender: UIButton) { if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) { // This is where I need help } }
我已经在这里回答了如何使用AVCapturePhotoOutput,但是我不明白如何在该代码中合并该代码,因为它涉及到声明一个新类。
你快到了。
AVCapturePhotoOutput
查看AVCapturePhotoOutput 文档以获取更多帮助。
这些是拍摄照片的步骤。
AVCapturePhotoSettings
capturePhoto(with:delegate:)
AVCapturePhotoCaptureDelegate
在您的clickCapture方法上具有以下代码,并且不要忘记在类中进行确认和实现委托。
clickCapture
let settings = AVCapturePhotoSettings() let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first! let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType, kCVPixelBufferWidthKey as String: 160, kCVPixelBufferHeightKey as String: 160, ] settings.previewPhotoFormat = previewFormat self.cameraOutput.capturePhoto(with: settings, delegate: self)
AVCaptureStillImageOutput
如果您打算通过视频连接拍摄照片。您可以按照以下步骤操作。
步骤1:建立连线
if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) { // ... // Code for photo capture goes here... }
步骤2:拍摄相片
captureStillImageAsynchronouslyFromConnection
stillImageOutput
sampleBuffer
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in // ... // Process the image data (sampleBuffer) here to get an image file we can put in our captureImageView })
步骤3:处理图像数据
if sampleBuffer != nil { let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) let dataProvider = CGDataProviderCreateWithCFData(imageData) let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault) let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right) // ... // Add the image to captureImageView here... }
步骤4:储存图片
根据您的需要将图像保存到照片库或在图像视图中显示
有关更多详细信息,请查看 “ 捕捉照片” 下的“ 创建自定义相机视图指南 ”。 __