GithubHelp home page GithubHelp logo

pierreveron / swiftuicam Goto Github PK

View Code? Open in Web Editor NEW
90.0 3.0 20.0 110 KB

A Snapchat Style Camera View to use with a SwiftUI interface

License: MIT License

Swift 100.00%
camera swiftui swift-package-manager snapchat

swiftuicam's Introduction

Platform: iOS 13.0+ Language: Swift 5.1

SwiftUICam

If you want to have a custom camera using SwiftUI and not using the UIPickerController that will display the original iOS camera, but don’t have time to play with AVFoundation, this package is for you!

SwiftUICam gives you a realtime full screen Snapchat-style view of the iPhone camera. Then, it is your job to built the interface you want and to connect it to the Camera View.

Features

SwiftUICam
😎 Snapchat-style media capture
πŸ“· Image capture
πŸŽ₯ Video capture
πŸŽ‰ Front and rear camera support
πŸ”¦ Front and rear flash
β˜€οΈ Retina flash support
πŸ”Ž Supports manual zoom
πŸ”’ Supports manual focus

Requirements

iOS 13.0+

Credits

It’s inspired by the project SwiftyCam made for UIKit: https://github.com/Awalz/SwiftyCam

License

This software is released under the MIT License, see LICENSE.txt.

Installation

Swift Package Manager:

SwiftUICam is available through SPM. To install it, go to File -> Swift Packages -> Add Package Dependency

And enter

https://github.com/pierreveron/SwiftUICam

As the url.

Manual installation:

Simply copy the contents of the Source folder into your project.

Usage

Prerequisites:

As of iOS 10, Apple requires the additon of the NSCameraUsageDescription and NSMicrophoneUsageDescription strings to the info.plist of your application. Example:

<key>NSCameraUsageDescription</key>
	<string>To Take Photos and Video</string>
<key>NSMicrophoneUsageDescription</key>
	<string>To Record Audio With Video</string>

Getting Started:

In your SwiftUI view simply add the CameraView, pass it the applicationName and add an @ObservedObject UserEvents that will be pass to the interface and the CameraViewRepresentable like this:

import SwiftUI
import SwiftUICam

struct ContentView: View {
    @ObservedObject events = UserEvents()
    var body: some View {
    	ZStack {
              CameraView(events: events, applicationName: "SwiftUICam")
	      CameraInterfaceView(events: events)
    	}
    }
}

Interface view

Make your interface view conform to the CameraActions protocol and add the @ObservedObject UserEvents property. Add gestures to your buttons that call the CameraActions functions and pass them the UserEvents property, simply as that.

import SwiftUI
import SwiftUICam

struct CameraInterfaceView: View, CameraActions {    
    @ObservedObject var events: UserEvents
    
    var body: some View {
        VStack {
            HStack {
                rotateButton().onTapGesture {
                    self.rotateCamera(events: events)
                }
                Spacer()
                flashButton().onTapGesture {
                    self.changeFlashMode(events: events)
                }
            }
            Spacer()
            captureButton().onTapGesture {
                self.takePhoto(events: events)
            }
        }
    }
}

CameraActions

It is the protocol that order the camera to take a picture or change the flash mode. List of the methods:

func takePhoto(events: UserEvents)
func toggleVideoRecording(events: UserEvents)
func rotateCamera(events: UserEvents)
func changeFlashMode(events: UserEvents)

The methods have a default definition to take make it easy to use.

Customize the CameraView

You can modify several properties of the CameraView on its initialization:

init(events: UserEvents, applicationName: String, preferredStartingCameraType: AVCaptureDevice.DeviceType = .builtInWideAngleCamera, preferredStartingCameraPosition: AVCaptureDevice.Position = .back, focusImage: String? = nil, pinchToZoom: Bool = true, tapToFocus: Bool = true, doubleTapCameraSwitch: Bool = true)
  • preferredStartingCameraType
  • preferredStartingCameraPosition
  • tapToFocus
  • focusImage
  • pinchToZoom
  • doubleTapCameraSwitch

Preferred Starting Options

By default, CameraView will launch to the back wide angle camera if it is available. This can be changed by changing the preferredStartingCameraType and the preferredStartingCameraPositionproperties to your desired ones.

TapToFocus

CameraView, by default, support tap to focus on the video preview. To disable this feature, pass to the tapToFocus property false.

FocusImage

When tapToFocus is enable, you can pass an UIImage that will be animate on the tap point.

PinchToZoom

CameraView, by default, support pinchToZoom on the front and back camera. The gestures work similar to the default iOS app and will zoom to the maximum supported zoom level. To disable this feature, pass to the pinchToZoom property false.

DoubleTapCameraSwitch

CameraView, by default, support double tap to switch camera. To disable this feature, pass to the doubleTapCameraSwitch property false.

What's next

  • Give more access to customization (max video duration, video quality, ...)
  • Add support for the device orientation
  • Add background audio support
  • Wait for Apple to release an update of SwiftUI to maybe make it simplier to use AVFoundation

swiftuicam's People

Contributors

pierreveron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

swiftuicam's Issues

hi,toggleVideoRecording is wrong

I found that the support for video recording in the code is poor, the behavior of toggleVideoRecording is wrong, and there is no function to start and stop recording. Can you update the next version to support, thank you

Video crashes on start

Crashes with error message:

Movie file finishing error: Optional(Error Domain=AVFoundationErrorDomain Code=-11859 "Movie recording cannot be started" UserInfo={NSUnderlyingError=0x281c02df0 {Error Domain=NSOSStatusErrorDomain Code=-16419 "(null)"}, NSLocalizedFailureReason=A movie recording is already in progress., NSLocalizedRecoverySuggestion=Stop the movie recording in progress and try again., AVErrorRecordingFailureDomainKey=1, NSLocalizedDescription=Movie recording cannot be started})

Crashes in this part of CameraViewController:

if error != nil { print("Movie file finishing error: \(String(describing: error))") success = (((error! as NSError).userInfo[AVErrorRecordingSuccessfullyFinishedKey] as AnyObject).boolValue)! }

Any idea as to why this is happening?

it starts new recording after the stop click

in CameraViewController

i think there has to be self.delegate?.didFinishVideoRecording() after the line 576 movieFileOutput.stopRecording()

and the lines from 662 to 664 is not needed
DispatchQueue.main.async {
self.delegate?.didFinishVideoRecording()
}

I am not sure why but I commented saving the video to camera roll code and this problem appeared
and the above solved it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.