I recently added a cameraView
to my project and everything works well.
Although right now i don't really capture photos yet and its what i want to implement next...
it would be great if you could show me how could i implement capturing a photo and previewing it with the code i have
so TakePhoto
function will capture the photo and then the image iv taken will be shown to me in the 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...
}
and i started with the preview page as well :
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.
}
}
thank you for your help
For
AVCaptureSession
, you can take a look at this. I've just created a sample of usingAVCaptureSession
which is simplification of AVCam Apple sample.Remember to set
Privacy - Camera Usage Description
in your.plist
.As there are some bugs making AVCaptureSession crash in iOS10 simulator, from this. I prefer to use
UIImagePickerController
. You can check out the following example: