如何拍摄照片并进行预览

时间:2017-07-02 02:53:43

标签: ios swift uiimageview avfoundation camera-view

我最近在我的项目中添加了cameraView,一切正常。 虽然现在我还没有真正拍摄照片以及我想要实现的内容......

如果你能告诉我如何实现拍摄照片并用我的代码预览它会很棒

因此TakePhoto函数会捕获照片,然后我会在previewPhotoViewController

中显示拍摄的图像

CustomCameraView:

import UIKit
import AVFoundation


class CustomCameraViewController: UIViewController,UIImagePickerControllerDelegate {

    @IBOutlet weak var cameraView: UIView!

    let session: AVCaptureSession = AVCaptureSession()


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        session.sessionPreset = AVCaptureSessionPresetHigh

        if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) {
            do {
                try session.addInput(AVCaptureDeviceInput(device: device))

            } catch {
                print(error.localizedDescription)
            }

            guard let previewLayer = AVCaptureVideoPreviewLayer(session: session) else {
                print("no preview layer")
                return
            }

            self.cameraView.layer.addSublayer(previewLayer)
            previewLayer.frame = self.view.layer.bounds

        }


        session.startRunning()

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidLoad() {
        super.viewDidLoad()

 }
    override var preferredStatusBarStyle : UIStatusBarStyle {
        return .lightContent
    }

    func TakePhoto(_ sender: UIButton) {

        //Wants to take photo with this button funcntion...


    }

我也开始使用预览页面:

PreviewPhotoViewController:

import UIKit

class PhotoPreviewViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    var takenPhoto :UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()

        if let availableImage = takenPhoto {
            imageView.image = availableImage
        }
}
    @IBAction func dismissView(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

对于AVCaptureSession,您可以查看this。我刚刚创建了一个使用AVCaptureSession的示例,它简化了AVCam Apple示例。

请务必在Privacy - Camera Usage Description中设置.plist

因为在iOS10模拟器中有一些错误导致AVCaptureSession崩溃,来自this。我更喜欢使用UIImagePickerController。您可以查看以下示例:

import UIKit
import AVFoundation
import MobileCoreServices

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!

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

    @IBAction func takePhoto(_ sender: UIButton) {
        self.presentPhotoCamera(canEdit: false)
    }

    func presentPhotoCamera(canEdit: Bool) {
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            return
        }

        let type = kUTTypeImage as String
        let imagePicker = UIImagePickerController()

        if UIImagePickerController.isSourceTypeAvailable(.camera) {

            if let availableTypes = UIImagePickerController.availableMediaTypes(for: .camera) {

                if availableTypes.contains(type) {

                    imagePicker.mediaTypes = [type]
                    imagePicker.sourceType = UIImagePickerControllerSourceType.camera
                }
            }

            if UIImagePickerController.isCameraDeviceAvailable(.rear) {
                imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.rear
            } else if UIImagePickerController.isCameraDeviceAvailable(.front) {
                imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
            }
        } else { return }

        imagePicker.allowsEditing = canEdit
        imagePicker.showsCameraControls = true
        imagePicker.delegate = self
        DispatchQueue.main.async {
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage

        picker.dismiss(animated: true, completion: {
            self.imageView.image = image
        })
    }
}