SwiftUICam is an AVFoundation based API that allows users to access the camera and its functions in a custom format on their iOS devices in SwiftUI with ease.
It allows users to:
- Take pictures
- Take Videos
- Save pictures + videos to camera roll
- Toggle Between Cameras
- Toggle Flash
- Swipe to zoom
Sections of this API were derived from Awalz/SwiftyCam and KavSoft's tutorial.
- Click here to see Awalz/SwiftyCam: https://github.com/Awalz/SwiftyCam
- Click here to see KavSoft: https://kavsoft.dev/SwiftUI_2.0/Custom_Camera/
This package is downloaded through Swift's Package Manager.
- Using XCode 11+ go to: File -> Swift Packages -> Add Package Dependency: https://github.com/vGebs/SwiftUICam
import SwiftUICam
let camera = CameraWrapper()
- To set the view, first create a structure that inhertits UIViewRepresentable.
- This protocol states that you must implement two functions - MakeUIView & UpdateUIView.
- MakeUIView - Sets the view (basically ViewDidLoad)
- UpdateUIView - Updates after user input (viewDidChange)
- To make these functions work with this framework, we will need to pass an instance of CameraWrapper and a view of type UIView.
- We also want to declare the a UIView variable that will be used to specify the frame in which this view will sit.
- Upon declaring these protocol functions and variables we can use the CameraWrapper to call the public preview setup functions as shown below.
import SwiftUICam
struct SwiftUICamPreview: UIViewRepresentable {
@ObservedObject var camera: CameraWrapper
var view: UIView
func makeUIView(context: Context) -> UIView {
return camera.makeUIView(view)
}
func updateUIView(_ uiView: UIView, context: Context) { }
}
For more information on how to setup/ use the interface, please reference Sources/SwiftUICam/SampleUsage
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This package is under the MIT license