如何在swift中将摄像机视图扩展到全屏 - UIImagePickerController

时间:2016-01-26 14:12:39

标签: ios swift camera

我正在使用UIImagePickerController拍摄带有照片的照片,并稍后在应用中使用该照片。

这是我启动相机的代码:

imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
imagePicker.showsCameraControls = false
imagePicker.cameraOverlayView = customViewTakePhoto()

presentViewController(imagePicker, animated: true, completion: nil)

一切正常,相机启动,我可以在拍摄后拍照,但只有一件我无法弄清楚,那就是:

如何在拍摄照片之前在整个屏幕上制作相机?

以下是显示我需要的图像

这是我现在的观点(我强烈推荐这本书,顺便说一下):

enter image description here

这就是我需要的(全屏相机视图):

enter image description here

3 个答案:

答案 0 :(得分:8)

import UIKit
import AVFoundation

class CameraViewController: UIViewController {

    // MARK: @IBOutlet

    @IBOutlet weak var overlayCamera: UIView! 
    @IBOutlet weak var capturedImage: UIImageView! 

    // MARK: Local Variables

    var captureSession: AVCaptureSession?
    var stillImageOutput: AVCaptureStillImageOutput?
    var previewLayer: AVCaptureVideoPreviewLayer?

    // MARK: Overrides

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        previewLayer!.frame = overlayCamera.bounds
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let tapGesture = UITapGestureRecognizer(target: self, action: "cameraDidTap")
        overlayCamera.addGestureRecognizer(tapGesture)
    }

    override func viewWillAppear(animated: Bool) {
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        super.viewWillAppear(animated)

        captureSession = AVCaptureSession()
        captureSession!.sessionPreset = AVCaptureSessionPresetPhoto

        let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

        var error: NSError?
        var input: AVCaptureDeviceInput!
        do {
            input = try AVCaptureDeviceInput(device: backCamera)
        } catch let error1 as NSError {
            error = error1
            input = nil
        }

        if error == nil && captureSession!.canAddInput(input) {
            captureSession!.addInput(input)

            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
                overlayCamera.layer.addSublayer(previewLayer!)

                captureSession!.startRunning()
            }
        }
    }

    override func viewWillDisappear(animated: Bool) {
        self.navigationController?.setNavigationBarHidden(false, animated: false)
        super.viewWillDisappear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // function for UITapGestureRecognizer for camera tap
    func cameraDidTap() {

        if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
            videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
            stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
                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)
                    self.capturedImage.image = image
                }
            })
        }
    }
}

答案 1 :(得分:2)

它对我有用:

设置AVCaptureSession()

let captureSession = AVCaptureSession()

guard let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back) else { return }
guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return }    
captureSession.addInput(input)
captureSession.startRunning()

设置AVCaptureVideoPreviewLayer()

let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)
previewLayer.frame = view.frame

答案 2 :(得分:0)

在PreviewLayer中,只需将videoGravity设置为AspectFill:

get => (decimal)someLong;