Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中使用AVCaptureSession捕捉图片?_Ios_Swift_Camera_Avcapturesession - Fatal编程技术网

Ios 如何在Swift中使用AVCaptureSession捕捉图片?

Ios 如何在Swift中使用AVCaptureSession捕捉图片?,ios,swift,camera,avcapturesession,Ios,Swift,Camera,Avcapturesession,我有一个UIViewController,其中我使用AVCaptureSession来显示相机,它工作得很好,速度很快。我将一个ui按钮对象放在这个相机视图的顶部,并为按钮添加了一个iAction 这就是它现在的样子: 现在,我想在用户点击按钮时获得当前相机视图的图片: @IBAction func takePicture(sender: AnyObject) { // omg, what do do?! } 我完全不知道该怎么做。我想象会有这样的事情: let captureSes

我有一个
UIViewController
,其中我使用
AVCaptureSession
来显示相机,它工作得很好,速度很快。我将一个
ui按钮
对象放在这个相机视图的顶部,并为按钮添加了一个
iAction

这就是它现在的样子:

现在,我想在用户点击按钮时获得当前相机视图的图片:

@IBAction func takePicture(sender: AnyObject) {
    // omg, what do do?!
}
我完全不知道该怎么做。我想象会有这样的事情:

let captureSession = AVCaptureSession()
var myDearPicture = captureSession.takePicture() as UIImage // something like it?

这里有控制器代码的完整链接,希望对AVCaptureSession示例有所帮助

import UIKit
import AVFoundation
class ViewController: UIViewController {
    let captureSession = AVCaptureSession()
    let stillImageOutput = AVCaptureStillImageOutput()
    var error: NSError?
    override func viewDidLoad() {
        super.viewDidLoad()
        let devices = AVCaptureDevice.devices().filter{ $0.hasMediaType(AVMediaTypeVideo) && $0.position == AVCaptureDevicePosition.Back }
        if let captureDevice = devices.first as? AVCaptureDevice  {

            captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &error))
            captureSession.sessionPreset = AVCaptureSessionPresetPhoto
            captureSession.startRunning()
            stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
            if captureSession.canAddOutput(stillImageOutput) {
                captureSession.addOutput(stillImageOutput)
            }
            if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
                previewLayer.bounds = view.bounds
                previewLayer.position = CGPointMake(view.bounds.midX, view.bounds.midY)
                previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                let cameraPreview = UIView(frame: CGRectMake(0.0, 0.0, view.bounds.size.width, view.bounds.size.height))
                cameraPreview.layer.addSublayer(previewLayer)
                cameraPreview.addGestureRecognizer(UITapGestureRecognizer(target: self, action:"saveToCamera:"))
                view.addSubview(cameraPreview)
            }
        }
    }
    func saveToCamera(sender: UITapGestureRecognizer) {
        if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
            stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
                (imageDataSampleBuffer, error) -> Void in
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
                 UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData), nil, nil, nil)
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
UIImagePickerController示例

import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    let imagePicker = UIImagePickerController()
    @IBOutlet weak var imageViewer: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
            dismissViewControllerAnimated(true, completion: nil)
             imageViewer.image = image
    }
    @IBAction func presentImagePicker(sender: AnyObject) {

        if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {

            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
            presentViewController(imagePicker, animated: true, completion: nil)

        }
    }
}

由于不推荐使用
AVCaptureStillImageOutput
,我创建了另一个在iOS 10中使用
AVCaptureSession
AVCapturePhotoOutput
的Swift示例。签出。

复制现有库,超级摄像头示例代码。。。问题是关于AVCaptureSession?为什么选择imagePicker?@JunchaoGu我已经添加了一个AVCaptureSession示例,我昨天已经用AVSession完成了捕获,但无论如何感谢你的回答@Leo Dabus有没有办法在设备更改为水平位置时更新预览层?@LeoDabus你能帮我吗?您可能希望包含示例代码中最相关的部分。