我最近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.
}
}
谢谢您的帮助