1

我最近cameraView在我的项目中添加了一个,一切正常。虽然现在我还没有真正拍摄照片,但这是我接下来要实现的......

如果您能告诉我如何实现拍摄照片并使用我拥有的代码进行预览,那就太好了

所以TakePhoto功能将捕获照片,然后拍摄的图像 iv 将在previewPhotoViewController

自定义相机视图:

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...
        
        
    }

我也从预览页面开始:

预览照片视图控制器:

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.
    }
    
}

谢谢您的帮助

4

1 回答 1

0

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

记得Privacy - Camera Usage Description在你的.plist.


由于在 iOS10 模拟器中存在一些导致 AVCaptureSession 崩溃的错误,因此来自. 我更喜欢使用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
        })
    }
}
于 2017-07-02T04:33:09.653 回答