Simple camera view which can take pictures and capture photos. Camera view crops pictures and videos with a given size. You can capture square videos. There are two Protocols which provide communication between your view controllers and fmcamera view. You can set maximum picture size and configure audio, video and picture settings if you want. Also you can get a thumbnail image for your video.
Built using XCode 12.5 (Swift 5)
To run the example project, clone the repo, and run pod install
from the Example directory first.
- Clone this repo
- Navigate to project folder
- Copy
Source
to your project
FMCamera is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'FMCamera'
Add the keys below to your info.plist
file. Don't forget to change the description texts.
<key>NSPhotoLibraryUsageDescription</key>
<string>Photo library usage description</string>
<key>NSMicrophoneUsageDescription</key>
<string>Microphone usage description</string>
<key>NSCameraUsageDescription</key>
<string>Camera usage description</string>
import FMCamera
in which class you want to use.- You can create a camera view on your storyboard or you can create it with code. Use Class named
FMCamera
- Update configuration parameters if necessary.
- Configure fmcamera with
configure()
function - Use picture, video or both delegates
- Write extensions for picture and/or video protocols.
- Protocol functions will return images or video urls, use them.
In your view controller after creating a FMCamera object, for example called fmCamera
override func viewDidLoad() {
super.viewDidLoad()
fmCapturePhotoDelegate = self
fmCaptureVideoDelegate = self
// You can update configuration parameters here
// or you can just use default ones.
fmCamera.configure()
}
// Take Photo
fmCamera.takePhoto()
// Capture Video
if !fmCamera.isCameraRecording {
fmCamera.startRecording()
} else {
fmCamera.stopRecording()
}
extension ViewController: FMCaptureVideoProtocol {
func recordingStarted(_ error: Error?) {
if let error = error {
print(error.localizedDescription)
}
}
func recordingFinished(_ videoUrl: URL?, _ error: Error?) {
if let error = error {
print(error.localizedDescription)
} else {
/// Use recorded video url
}
}
}
extension ViewController: FMCapturePhotoProtocol {
func captured(_ image: UIImage, data: Data) {
/// Use captured and optimized image
}
}
Usage examples are added to code documentation. Option+click on parameter to see usage examples.
We have two types of configuration parameters. Before calling fmcamera.configure()
and before taking a photo.
/// Set session preset with<br/>
var sPreset: AVCaptureSession.Preset = .medium
/// Set this parameter to TRUE if you want to use
/// FMCamera just for photo capturing.<br/>
var setForPhotoCapturingOnly: Bool = false
///Flash mode for capturing.<br/>
var flashMode: AVCaptureDevice.FlashMode = .auto
/// Capture photo format.<br/>
var photoSettingsFormat: [String: Any]?
/// Set default camera, front or back.<br/>
var captureDevicePosition: AVCaptureDevice.Position = .back
/// Video settings for recording video.<br/>
var videoSettings: [String: Any] = [:]
/// Audio settings for audio capturing<br/>
var audioSettings: [String: Any] = [:]
/// Update this for save the photo to your Photos.
var willSavePhotoToPhotos: Bool = false
/// Update this for save the video to your Photos.
var willSaveVideoToPhotos: Bool = false
/// Save original or save reduced image to photo roll.
///Works if `willSaveVideoToPhotos` is true.
var saveReducedImageToPhotos: Bool = false
/// Set max picture file size in bytes.
var maxPictureFileSize: Int = 400000
/// Set this to false if you want to optimize images yourself.
var optimizeImage: Bool = true
/// Decide if the captured photo rotates upwards.
///After capturing, photo always oriented upwards if you set this flag to true.
var rotateCapturedPhotoUpwards: Bool = false
https://stackoverflow.com/a/44917862
https://www.appcoda.com/avfoundation-swift-guide/
https://stackoverflow.com/a/32041649
gokhanmandaci, [email protected]
FMCamera is available under the MIT license. See the LICENSE file for more info.