https://github.com/edonv/avcaptureviewmodel
An ObservableObject view model for capturing photo/video on iOS for SwiftUI and UIKit.
https://github.com/edonv/avcaptureviewmodel
avfoundation camera capture ios movie observableobject observedobject photo stateobject swift swiftui uikit video viewmodel
Last synced: 12 months ago
JSON representation
An ObservableObject view model for capturing photo/video on iOS for SwiftUI and UIKit.
- Host: GitHub
- URL: https://github.com/edonv/avcaptureviewmodel
- Owner: edonv
- License: mit
- Created: 2023-08-19T17:05:44.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-21T15:43:16.000Z (about 2 years ago)
- Last Synced: 2025-03-06T13:19:50.688Z (about 1 year ago)
- Topics: avfoundation, camera, capture, ios, movie, observableobject, observedobject, photo, stateobject, swift, swiftui, uikit, video, viewmodel
- Language: Swift
- Homepage: https://swiftpackageindex.com/edonv/AVCaptureViewModel
- Size: 615 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AVCaptureViewModel
[](https://swiftpackageindex.com/edonv/AVCaptureViewModel)
[](https://swiftpackageindex.com/edonv/AVCaptureViewModel)
`AVCaptureViewModel` is an `ObservableObject` with exposed `@Published` properties to be used with the provided ``CameraPreview`` (SwiftUI) or ``CameraPreviewView`` (UIKit). It allows you to build a custom interface around them as part of a larger `View` or `UIViewController` (respectively) for capturing photos and videos.
It can be used to capture photos and videos to be immediately sent to the device's photo library, or you can subscribe to provided `@Published` properties to get the captures as they happen.
## Future and Next Steps
There is still more work to be done to provide more granular control, but it's already functional as it is.
### Things to add:
- [ ] Some kind of subscribable publisher (likely a `CurrentValueSubject`) for publishing events (such as photo capture starting/ending, etc)
- [ ] Exposing more properties for customizability
- [ ] Fix existing issues with screen rotation
- [ ] Implement @MainActor on `AVCaptureViewModel` and implement removal of `DispatchQueue.main.async { }`
- [ ] Figure out how to automatically resize `CameraPreviewView` to fit the size of the preview (i.e. when it's in photo mode, it doesn't fill the space)
- [x] Add a function for pinch gesture/zoom
## How to Use `AVCaptureViewModel`
*COMING SOON*
## Misc Notes
- The framework doesn't currently support a built-in way to keep the screen from rotating while recording a video, as this isn't possible the way it's written. So if your app allows for multiple orientations, you'll have to listen for event changes (specifically `.movieRecordingStarted` and `.movieRecordingFinished`) and figure it out on your own. In UIKit, this is done by setting or overriding a `UIViewController`'s [`supportedInterfaceOrientations`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations) property. There isn't currently a way to limit app rotations per view in SwiftUI.
## Credit
Most of the behind-scenes-code stems directly from Apple's [AVCam tutorial](https://developer.apple.com/documentation/avfoundation/capture_setup/avcam_building_a_camera_app). I started with the current version of it (parts of which require iOS 17/Xcode 15), and I made it backwards-compatible to iOS 13. I also reworked parts of the code to work with properties of `AVCaptureViewModel`, as well as to replace `@IBAction`s and `@IBOutlets`. Additionally, I turned their `PreviewView` into `CameraPreviewView` and wrapped it with `CameraPreview`.