I am using UIImagePickerController to snap a photo with a pone and use that photo in the app later.
This is my code for starting a camera:
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
imagePicker.showsCameraControls = false
imagePicker.cameraOverlayView = customViewTakePhoto()
presentViewController(imagePicker, animated: true, completion: nil)
It all works fine, the camera starts, I am able to fetch photo once it's taken, but there is only one thing I cannot figure out, and that is:
How to make camera view across the entire screen, before the photo is taken?
Here are the images showing exactly what I need
This is my View as it is right now (I highly recommend the book, btw):
and this is what I need (Fullscreen camera view):
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
}
})
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With