
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
How to Add Live Camera Preview to UIView in Swift?
To add a live camera preview to our default UIView in swift we can either use AVFoundation framework of iOS SDK or native UIImagePickerController(). In this example we’ll be using ImagePicker as our aim is to present camera preview on the UIView and Imagepicker is suitable for that task. AVFoundation can be used when we need a lot of customization on our camera or different types of custom actions.
To show a camera preview on the UIView we need to perform the following steps.
Create a UIImagePickerController object.
Conform our class to UIImagePickerControllerDelegate and UINavigationControllerDelegate.
Assign delegates to the object we created in step one.
Add the object we created as a child
Add the child's view as a subView to our UIView.
To achieve the same we’ll use the code below
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib let imgPicker = UIImagePickerController() if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerController.CameraDevice.front) { imgPicker.delegate = self imgPicker.sourceType = .camera addChild(imgPicker) self.view.addSubview(imgPicker.view) imgPicker.view.frame = self.view.bounds imgPicker.allowsEditing = false imgPicker.showsCameraControls = false imgPicker.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] } }
For this example we have not written any function and we directly created objects here, but it’s always a good choice to use lazy variables in situations where we might not need to use some variable.
This code will not work on a simulator as it makes use of camera. When I run the above code on an iPhone 7+, below is the result that’s produced.
- Related Articles
- How to use the front camera in Swift?
- How to add and use camera tool in Excel
- How add 1 day to Date in swift?
- How to Add two Numbers in Swift Program?
- How to add shadow on text swift?
- How to add constraints programmatically using Swift
- Capture picture from iOS camera using Swift
- How to enable live chat in live streaming on YouTube?
- How to click camera programmatically in android?
- Add an element to an array in Swift
- How to click a camera programmatically in Android?
- How to preview an image before it is uploaded in JavaScript?
- How To Change UIView's Border Color And Thickness in Cocoa Touch?
- How to create a Border, Border radius, and shadow to a UIView in iPhone/iOS?
- How to work with Camera in an Android App?
