DOWNLOAD RESOURCES: https://ductran.co/p/38
In iOS and Swift, in order to show up a camera and take a photo or record a video, we can use the default UIImagePickerController.
But sometimes, you want to create something minimalistic, simpler with custom design.
So let's do that in this training. You'll learn:
How to use AVKit and AVFoundation
Configure AVCaptureSession and AVCaptureDevice to take photos
Add UITapGestureRecognizer to switch from front facing camera to back facing camera
Save the taken photo to photos library
Let's get started by downloading all the resources I prepared for you in this episode below!
DOWNLOAD RESOURCES: https://ductran.co/p/38
*********
ABOUT CODE MASTERY
*********
Code Mastery is hosted by Duc Tran, founder of Developers Academy.
This is his free-style no notes, no teleprompter presentation and live coding broadcast with you guys everyday.
To join Duc's free courses, register for free at http://ductran.co/
*********
MEET DUC TRAN
*********
Duc Tran is founder of Developers Academy, one of the world's leading iOS, Android and Web development trainers.
More than 2,000,000 developers have studied his video trainings; 100,000 developers see his posts each month. Each year, Duc has helped 20,000 plus developers graduate from his online courses or video series.
*********
FREE TRAININGS IN IOS DEVELOPMENT
*********
To subscribe and get free tutorials, courses and weekly content, visit me at: http://ductran.co/
Connect with Duc on facebook: / ductranfan
Tweet him: / ductrongtran
Get daily inspiration: / ductran.co
*********
SOURCE CODE IN THIS COURSE
*********
import UIKit
import AVFoundation
class CameraViewController : UIViewController
{
@IBOutlet weak var cameraButton: UIButton!
var captureSession = AVCaptureSession()
// which camera input do we want to use
var backFacingCamera: AVCaptureDevice?
var frontFacingCamera: AVCaptureDevice?
var currentDevice: AVCaptureDevice?
// output device
var stillImageOutput: AVCaptureStillImageOutput?
var stillImage: UIImage?
// camera preview layer
var cameraPreviewLayer: AVCaptureVideoPreviewLayer?
// double tap to switch from back to front facing camera
var toggleCameraGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as! [AVCaptureDevice]
for device in devices {
if device.position == .back {
backFacingCamera = device
} else if device.position == .front {
frontFacingCamera = device
}
}
// default device
currentDevice = frontFacingCamera
// configure the session with the output for capturing our still image
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice)
captureSession.addInput(captureDeviceInput)
captureSession.addOutput(stillImageOutput)
// set up the camera preview layer
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(cameraPreviewLayer!)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
cameraPreviewLayer?.frame = view.layer.frame
view.bringSubview(toFront: cameraButton)
captureSession.startRunning()
// toggle the camera
toggleCameraGestureRecognizer.numberOfTapsRequired = 2
toggleCameraGestureRecognizer.addTarget(self, action: #selector(toggleCamera))
view.addGestureRecognizer(toggleCameraGestureRecognizer)
} catch let error {
print(error)
}
}
@objc private func toggleCamera() {
// start the configuration change
captureSession.beginConfiguration()
let newDevice = (currentDevice?.position == . back) ? frontFacingCamera : backFacingCamera
for input in captureSession.inputs {
captureSession.removeInput(input as! AVCaptureDeviceInput)
}
let cameraInput: AVCaptureDeviceInput
do {
cameraInput = try AVCaptureDeviceInput(device: newDevice)
} catch let error {
print(error)
return
}
if captureSession.canAddInput(cameraInput) {
captureSession.addInput(cameraInput)
}
currentDevice = newDevice
captureSession.commitConfiguration()
}
Смотрите видео BUILD SNAPCHAT CAMERA IN iOS WITH SWIFT - AVKIT AND AVFOUNDATION онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал David Tran 07 Сентябрь 2017. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 16,993 раз и оно понравилось 193 посетителям.