This is a simple Swift class to provide all the configurations you need to create custom camera view in your app. It follows orientation change and updates UI accordingly, supports front and rear camera selection, pinch to zoom, tap to focus, exposure slider, different flash modes, inputs and outputs. Just drag, drop and use.
The easiest way to install the CameraManager is with CocoaPods
use_frameworks!
pod 'CameraManager', '~> 4.2'The Swift Package Manager is a tool for managing the distribution of Swift code.
Add CameraManager as a dependency in your Package.swift file:
import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/imaginary-cloud/CameraManager", majorVersion: 4, minor: 2)
]
)
Carthage is another dependency management tool written in Swift.
Add the following line to your Cartfile:
github "imaginary-cloud/CameraManager" >= 4.2
And run carthage update to build the dynamic framework.
To use it you just add the preview layer to your desired view, you'll get back the state of the camera if it's unavailable, ready or the user denied access to it. Have in mind that in order to retain the AVCaptureSession you will need to retain cameraManager instance somewhere, ex. as an instance constant.
let cameraManager = CameraManager()
cameraManager.addPreviewLayerToView(self.cameraView)To shoot image all you need to do is call:
cameraManager.capturePictureWithCompletion({ (image, error) -> Void in
self.myImage = image
})To record video you call:
cameraManager.startRecordingVideo()
cameraManager.stopVideoRecording({ (videoURL, error) -> Void in
NSFileManager.defaultManager().copyItemAtURL(videoURL, toURL: self.myVideoURL, error: &error)
})To zoom in manually:
let zoomScale = CGFloat(2.0)
cameraManager.zoom(zoomScale)You can set input device to front or back camera. (Default: .Back)
cameraManager.cameraDevice = .Front || .BackYou can specify if the front camera image should be horizontally fliped. (Default: false)
cameraManager.shouldFlipFrontCameraImage = true || falseYou can enable or disable gestures on camera preview. (Default: true)
cameraManager.shouldEnableTapToFocus = true || false
cameraManager.shouldEnablePinchToZoom = true || false
cameraManager.shouldEnableExposure = true || falseYou can set output format to Image, video or video with audio. (Default: .StillImage)
cameraManager.cameraOutputMode = .StillImage || .VideoWithMic || .VideoOnlyYou can set the quality. (Default: .High)
cameraManager.cameraOutputQuality = .Low || .Medium || .HighYou can specify the focus mode. (Default: .continuousAutoFocus)
cameraManager.focusMode = .autoFocus || .continuousAutoFocus || .lockedYou can specifiy the exposure mode. (Default: .continuousAutoExposure)
cameraManager.exposureMode = .autoExpose || .continuousAutoExposure || .locked || .customYou can change the flash mode (it will also set corresponding flash mode). (Default: .Off)
cameraManager.flashMode = .Off || .On || .AutoYou can enable location services for storing in Camera Roll. (Default: false)
cameraManager.shouldUseLocationServices = true || falseYou can specify if you want to save the files to phone library. (Default: true)
cameraManager.writeFilesToPhoneLibrary = true || falseYou can specify the album names for image and video recordings.
cameraManager.imageAlbumName = "Image Album Name"
cameraManager.videoAlbumName = "Video Album Name" You can specify if you want to disable animations. (Default: true)
cameraManager.animateShutter = true || false
cameraManager.animateCameraDeviceChange = true || falseYou can specify if you want the user to be asked about camera permissions automatically when you first try to use the camera or manually. (Default: true)
cameraManager.showAccessPermissionPopupAutomatically = true || falseTo check if the device supports flash call:
cameraManager.hasFlashTo change flash mode to the next available one you can use this handy function which will also return current value for you to update the UI accordingly:
cameraManager.changeFlashMode()You can even setUp your custom block to handle error messages: It can be customized to be presented on the Window root view controller, for example.
cameraManager.showErrorBlock = { (erTitle: String, erMessage: String) -> Void in
var alertController = UIAlertController(title: erTitle, message: erMessage, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in
}))
let topController = UIApplication.sharedApplication().keyWindow?.rootViewController
if (topController != nil) {
topController?.presentViewController(alertController, animated: true, completion: { () -> Void in
//
})
}
}Supports iOS 9 and above. Xcode 9.0 is required to build the latest code written in Swift 4.0.
Now it's compatible with latest Swift syntax, so if you're using any Swift version prior to 4.0 make sure to use one of the previously tagged releases (i.e for Swift 3.0 see v3.2.0).
Copyright © 2010-2018 Imaginary Cloud. This library is licensed under the MIT license.
We apply our own Product Design Process to bring great digital products to life. Visit our website to find out about our other projects or contact us if there's an idea that you want to turn into an outstanding product, we'll take it from there!