Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 188.8.131.52 release ready!
Updated 27 September 2021
Accessing the built-in Image Picker Controller is a quick and easy way to get image and video capture into your app. However, when you need style and functionality that goes beyond the stock Image Picker Controller you will need to create a Custom Camera View.
Add the following view elements to the ViewController in Storyboard:
At the top of your ViewController file, import AVFoundation
Create Outlets for the UIView and UIImageView.
Create an Action for the UIButton.
Above the viewDidLoad method, where you create variables you want to be accessible anywhere in the ViewController file, create the following Instance Variables.
The bulk of the camera setup will happen in the viewDidAppear.
The session will coordinate the input and output data from the device’s camera.Still in viewDidAppear
In this example, we will be using the rear camera. The front camera and microphone are additional input devices at your disposal. Printing debug comment incase the fetching the rear camera fails. Still in viewDidAppear
We now need to make an AVCaptureDeviceInput. The AVCaptureDeviceInput will serve as the “middle man” to attach the input device, backCamera to the session.
Just like we created an AVCaptureDeviceInput to be the “middle man” to attach the input device, we will use AVCapturePhotoOutput to help us attach the output to the session.
If there are no errors from our last step and the session is able to accept input and output, the go-ahead and add input add output to the Session.
Now that the input and output are all hooked up with our session, we just need to get our Live Preview going so we can actually display what the camera sees on the screen in our UIView, previewView.
We need to call -startRunning on the session to start the live view. However, -startRunning is a blocking method which means it will block the UI if it’s running on the main thread. If the session takes a while to start, users would want the UI to be responsive and cancel out of the camera view.
Once the live view starts let’s set the Preview layer to fit, but we must return to the main thread to do so!
Let’s create an IBAction of the Take photo Button and capture a JPEG by calling our instance of AVCapturePhotoOutput or stillImageOut the method func capturePhoto(with:, delegate:) or -capturePhotoWithSettings:delegate:. This method requires us to provide it with a setting and a delegate to deliver the captured photo. This delegate will be this ViewController so we also need to conform to the protocol AVCapturePhotoCaptureDelegate
Take photo Button
func capturePhoto(with:, delegate:)
The AVCapturePhotoOutput will deliver the captured photo to the assigned delegate which is our current ViewController by a delegate method called photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?). The photo is delivered to us as an AVCapturePhoto which is easy to transform into Data/NSData and then into UIImage.
photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?)
Let’s not forget to stop the session when we leave the camera view!
NOTE: The simulator does NOT have a camera so you need to run your app on an Actual Device to see the magic!
You can also know more from here!!!
Thanks for Reading!!!
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.