简体   繁体   中英

Swiftycam media capture library delegate crashes

I am using this library called SwiftyCam As it is mentioned in its page, this part is a bit confusing:

SwiftyCam is a drop-in convenience framework. To create a Camera instance, create a new UIViewController subclass. Replace the UIViewController subclass declaration with SwiftyCamViewController.

So i created a new view controller extends class SwiftyCameraViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate as in sample project. And i called this class from my main UIViewcontroller(MainViewController.swift) as:

let swiftyCamera = SwiftyCameraViewController()
self.present(swiftyCamera, animated: true, completion: nil)

So, this is SwiftyCameraViewController:

import UIKit

class SwiftyCameraViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {

@IBOutlet weak var captureButton: SwiftyRecordButton!
@IBOutlet weak var flipCameraButton: UIButton!
@IBOutlet weak var flashButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    cameraDelegate = self
    maximumVideoDuration = 10.0
    shouldUseDeviceOrientation = true
    allowAutoRotate = true
    audioEnabled = true
}

override var prefersStatusBarHidden: Bool {
    return true
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    captureButton.delegate = self
}

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didTake photo: UIImage) {
    let newVC = PhotoViewController(image: photo)
    self.present(newVC, animated: true, completion: nil)
}

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didBeginRecordingVideo camera: SwiftyCamViewController.CameraSelection) {
    print("Did Begin Recording")
    captureButton.growButton()
    UIView.animate(withDuration: 0.25, animations: {
        self.flashButton.alpha = 0.0
        self.flipCameraButton.alpha = 0.0
    })
}

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFinishRecordingVideo camera: SwiftyCamViewController.CameraSelection) {
    print("Did finish Recording")
    captureButton.shrinkButton()
    UIView.animate(withDuration: 0.25, animations: {
        self.flashButton.alpha = 1.0
        self.flipCameraButton.alpha = 1.0
    })
}

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFinishProcessVideoAt url: URL) {
    let newVC = VideoViewController(videoURL: url)
    self.present(newVC, animated: true, completion: nil)
}

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFocusAtPoint point: CGPoint) {
    let focusView = UIImageView(image: #imageLiteral(resourceName: "focus"))
    focusView.center = point
    focusView.alpha = 0.0
    view.addSubview(focusView)

    UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: {
        focusView.alpha = 1.0
        focusView.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
    }, completion: { (success) in
        UIView.animate(withDuration: 0.15, delay: 0.5, options: .curveEaseInOut, animations: {
            focusView.alpha = 0.0
            focusView.transform = CGAffineTransform(translationX: 0.6, y: 0.6)
        }, completion: { (success) in
            focusView.removeFromSuperview()
        })
    })
}

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didChangeZoomLevel zoom: CGFloat) {
    print(zoom)
}

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didSwitchCameras camera: SwiftyCamViewController.CameraSelection) {
    print(camera)
}

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFailToRecordVideo error: Error) {
    print(error)
}

@IBAction func cameraSwitchTapped(_ sender: Any) {
    switchCamera()
}

@IBAction func toggleFlashTapped(_ sender: Any) {
    flashEnabled = !flashEnabled

    if flashEnabled == true {
        flashButton.setImage(#imageLiteral(resourceName: "flash"), for: UIControlState())
    } else {
        flashButton.setImage(#imageLiteral(resourceName: "flashOutline"), for: UIControlState())
    }
}
}

It crashed at line 33: captureButton.delegate = self, because captureButton is nil

I appreciate any kind of help. Thank you.

You must load the viewController with identifier

let swiftyCamera = self.storyboard?.instantiateViewController(withIdentifier:"swiftyCameraID")
self.present(swiftyCamera, animated: true, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM