GithubHelp home page GithubHelp logo

eewilfred / avcapture-photo-helper Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 43 KB

Protocol-based easy setup for enabling live preview and capture from camera

License: The Unlicense

Swift 100.00%
swift avfou avca avcapturephotooutput photo-ca ios ios-swift ios-camera ios-video-support

avcapture-photo-helper's Introduction

AV-Capture-Photo-Helper

Protocol-based easy setup for enabling live preview and capture from camera

Usage:

Step 1: Confirm your view / view controller to LivePhotoCapture

class ViewController: UIViewController, LivePhotoCapture {}

Step 2: Allow xcode to fill all needed variables and methodes

step 3: add intilizers (if you dont need custom intilizer use the example code)

class ViewController: UIViewController, LivePhotoCapture {

    var captureSession: AVCaptureSession?
    var photoOutput: AVCapturePhotoOutput?
    var cameraPreviewLayer: AVCaptureVideoPreviewLayer?

    // This method has to be implemented in the caller class as its @obj func and swift limits usage of this in side protocol extenstions
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {}
}

step 4: in the view didload or the place were you are plannig to start live preview by calling prepareForPhotoCapture(onView: UIView?, cameraPosition: AVCaptureDevice.Position = .unspecified)

override func viewDidLoad() {

    super.viewDidLoad()
    prepareForPhotoCapture(onView: cameraPreviewView)
}

step 5: To capture Image call capturePhoto(), once processed you will get call back on, photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) if AVCapturePhoto needs to be converted to UIIMage call processPhoto( photo: AVCapturePhoto) -> UIImage?

class ViewController: UIViewController, LivePhotoCapture {

    var captureSession: AVCaptureSession?
    var photoOutput: AVCapturePhotoOutput?
    var cameraPreviewLayer: AVCaptureVideoPreviewLayer?

    // This method has to be implemented in the caller class as its @obj func and swift limits usage of this in side protocol extenstions
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

        // if user is needs a processed UIImage
        let image = processPhoto(photo: photo)
    }

    func capturePhoto(_ sender: UIButton) {

        capturePhoto()
    }
}

step 5: if View is getting dissapeared or needs to stop live preview, call stopSession()

    override func viewWillDisappear(_ animated: Bool) {
    
        super.viewWillDisappear(animated)
        stopSession()
    }

step 6: if live preview needs to be restarted, call startSession()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        startSession()
    }

On your View

For detailed information on how to add prototcol on view follow sample application VideoView.swift


Note

Keep in mind that if we are adding preview to a view's layer all the subviews on it may not be visible, to avoid this please add a seprate view which can be used as a canvas for preview, Refer Demo app for more details

avcapture-photo-helper's People

Contributors

mredwinwilson avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

jasonk2k

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.