I am trying to incorporate a framework called YPImagePicker I found on GitHub into an app I am developing to teach myself to code. The framework is great for selecting an image, however, I am trying to have the image selected appear on the next view controller when done is clicked.
YPPickerVC
Below is the code from the YPPickerVC that I have edited to attempt to send the photo selected to my other View Controller named PostStatusVC. Mainly I have made edits in the done() function where I am trying to push the new view controller.
import Foundation
import Stevia
import Photos
protocol ImagePickerDelegate: AnyObject {
func noPhotos()
func shouldAddToSelection(indexPath: IndexPath, numSelections: Int) -> Bool
}
open class YPPickerVC: YPBottomPager, YPBottomPagerDelegate {
let albumsManager = YPAlbumsManager()
var shouldHideStatusBar = false
var initialStatusBarHidden = false
weak var imagePickerDelegate: ImagePickerDelegate?
override open var prefersStatusBarHidden: Bool {
return (shouldHideStatusBar || initialStatusBarHidden) && YPConfig.hidesStatusBar
}
/// Private callbacks to YPImagePicker
public var didClose:(() -> Void)?
public var didSelectItems: (([YPMediaItem]) -> Void)?
enum Mode {
case library
case camera
case video
}
private var libraryVC: YPLibraryVC?
private var cameraVC: YPCameraVC?
private var videoVC: YPVideoCaptureVC?
var mode = Mode.camera
var capturedImage: UIImage?
open override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = YPConfig.colors.safeAreaBackgroundColor
delegate = self
// Force Library only when using `minNumberOfItems`.
if YPConfig.library.minNumberOfItems > 1 {
YPImagePickerConfiguration.shared.screens = [.library]
}
// Library
if YPConfig.screens.contains(.library) {
libraryVC = YPLibraryVC()
libraryVC?.delegate = self
}
// Camera
if YPConfig.screens.contains(.photo) {
cameraVC = YPCameraVC()
cameraVC?.didCapturePhoto = { [weak self] img in
self?.didSelectItems?([YPMediaItem.photo(p: YPMediaPhoto(image: img,
fromCamera: true))])
}
}
// Video
if YPConfig.screens.contains(.video) {
videoVC = YPVideoCaptureVC()
videoVC?.didCaptureVideo = { [weak self] videoURL in
self?.didSelectItems?([YPMediaItem
.video(v: YPMediaVideo(thumbnail: thumbnailFromVideoPath(videoURL),
videoURL: videoURL,
fromCamera: true))])
}
}
// Show screens
var vcs = [UIViewController]()
for screen in YPConfig.screens {
switch screen {
case .library:
if let libraryVC = libraryVC {
vcs.append(libraryVC)
}
case .photo:
if let cameraVC = cameraVC {
vcs.append(cameraVC)
}
case .video:
if let videoVC = videoVC {
vcs.append(videoVC)
}
}
}
controllers = vcs
// Select good mode
if YPConfig.screens.contains(YPConfig.startOnScreen) {
switch YPConfig.startOnScreen {
case .library:
mode = .library
case .photo:
mode = .camera
case .video:
mode = .video
}
}
// Select good screen
if let index = YPConfig.screens.firstIndex(of: YPConfig.startOnScreen) {
startOnPage(index)
}
YPHelper.changeBackButtonIcon(self)
YPHelper.changeBackButtonTitle(self)
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
cameraVC?.v.shotButton.isEnabled = true
updateMode(with: currentController)
}
open override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
shouldHideStatusBar = true
initialStatusBarHidden = true
UIView.animate(withDuration: 0.3) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
internal func pagerScrollViewDidScroll(_ scrollView: UIScrollView) { }
func modeFor(vc: UIViewController) -> Mode {
switch vc {
case is YPLibraryVC:
return .library
case is YPCameraVC:
return .camera
case is YPVideoCaptureVC:
return .video
default:
return .camera
}
}
func pagerDidSelectController(_ vc: UIViewController) {
updateMode(with: vc)
}
func updateMode(with vc: UIViewController) {
stopCurrentCamera()
// Set new mode
mode = modeFor(vc: vc)
// Re-trigger permission check
if let vc = vc as? YPLibraryVC {
vc.checkPermission()
} else if let cameraVC = vc as? YPCameraVC {
cameraVC.start()
} else if let videoVC = vc as? YPVideoCaptureVC {
videoVC.start()
}
updateUI()
}
func stopCurrentCamera() {
switch mode {
case .library:
libraryVC?.pausePlayer()
case .camera:
cameraVC?.stopCamera()
case .video:
videoVC?.stopCamera()
}
}
open override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
shouldHideStatusBar = false
stopAll()
}
@objc
func navBarTapped() {
let vc = YPAlbumVC(albumsManager: albumsManager)
let navVC = UINavigationController(rootViewController: vc)
navVC.navigationBar.tintColor = .ypLabel
vc.didSelectAlbum = { [weak self] album in
self?.libraryVC?.setAlbum(album)
self?.setTitleViewWithTitle(aTitle: album.title)
navVC.dismiss(animated: true, completion: nil)
}
present(navVC, animated: true, completion: nil)
}
func setTitleViewWithTitle(aTitle: String) {
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
let label = UILabel()
label.text = aTitle
// Use YPConfig font
label.font = YPConfig.fonts.pickerTitleFont
// Use custom textColor if set by user.
if let navBarTitleColor = UINavigationBar.appearance().titleTextAttributes?[.foregroundColor] as? UIColor {
label.textColor = navBarTitleColor
}
if YPConfig.library.options != nil {
titleView.sv(
label
)
|-(>=8)-label.centerHorizontally()-(>=8)-|
align(horizontally: label)
} else {
let arrow = UIImageView()
arrow.image = YPConfig.icons.arrowDownIcon
arrow.image = arrow.image?.withRenderingMode(.alwaysTemplate)
arrow.tintColor = .ypLabel
let attributes = UINavigationBar.appearance().titleTextAttributes
if let attributes = attributes, let foregroundColor = attributes[.foregroundColor] as? UIColor {
arrow.image = arrow.image?.withRenderingMode(.alwaysTemplate)
arrow.tintColor = foregroundColor
}
let button = UIButton()
button.addTarget(self, action: #selector(navBarTapped), for: .touchUpInside)
button.setBackgroundColor(UIColor.white.withAlphaComponent(0.5), forState: .highlighted)
titleView.sv(
label,
arrow,
button
)
button.fillContainer()
|-(>=8)-label.centerHorizontally()-arrow-(>=8)-|
align(horizontally: label-arrow)
}
label.firstBaselineAnchor.constraint(equalTo: titleView.bottomAnchor, constant: -14).isActive = true
titleView.heightAnchor.constraint(equalToConstant: 40).isActive = true
navigationItem.titleView = titleView
}
func updateUI() {
if !YPConfig.hidesCancelButton {
// Update Nav Bar state.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: YPConfig.wordings.cancel,
style: .plain,
target: self,
action: #selector(close))
}
switch mode {
case .library:
setTitleViewWithTitle(aTitle: libraryVC?.title ?? "")
navigationItem.rightBarButtonItem = UIBarButtonItem(title: YPConfig.wordings.next,
style: .done,
target: self,
action: #selector(done))
navigationItem.rightBarButtonItem?.tintColor = YPConfig.colors.tintColor
// Disable Next Button until minNumberOfItems is reached.
navigationItem.rightBarButtonItem?.isEnabled =
libraryVC!.selection.count >= YPConfig.library.minNumberOfItems
case .camera:
navigationItem.titleView = nil
title = cameraVC?.title
navigationItem.rightBarButtonItem = UIBarButtonItem(title: YPConfig.wordings.next,
style: .done,
target: self,
action: #selector(done))
case .video:
navigationItem.titleView = nil
title = videoVC?.title
navigationItem.rightBarButtonItem = nil
}
navigationItem.rightBarButtonItem?.setFont(font: YPConfig.fonts.rightBarButtonFont, forState: .normal)
navigationItem.rightBarButtonItem?.setFont(font: YPConfig.fonts.rightBarButtonFont, forState: .disabled)
navigationItem.leftBarButtonItem?.setFont(font: YPConfig.fonts.leftBarButtonFont, forState: .normal)
}
@objc
func close() {
// Cancelling exporting of all videos
if let libraryVC = libraryVC {
libraryVC.mediaManager.forseCancelExporting()
}
self.didClose?()
}
// When pressing "Next"
@objc
func done() {
guard let libraryVC = libraryVC else { print("?? YPPickerVC >>> YPLibraryVC deallocated"); return }
//showPicker()
/*let vc = PostStatusVC()
//vc.title = "Settings"
vc.selectedImage = photo
navigationController?.pushViewController(vc, animated: true)*/
var config = YPImagePickerConfiguration()
let picker = YPImagePicker(configuration: config)
picker.didFinishPicking { [unowned
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…