如何修改Fusuma代码实现选图/拍照后跳转至新ViewController?
Absolutely! You can achieve both requirements with minimal modifications to Fusuma. Let's break this down into two clear, actionable steps:
Fusuma provides delegate methods to notify you when a photo is selected or captured. Here's how to use them to trigger the navigation:
- First, make sure your view controller conforms to
FusumaDelegate:
class YourViewController: UIViewController, FusumaDelegate { // ... existing view controller code ... }
- When presenting Fusuma, set its delegate to your view controller:
let fusuma = FusumaViewController() fusuma.delegate = self present(fusuma, animated: true)
- Implement the
fusumaImageSelected(_:source:)delegate method. This is where you'll receive the captured/selected image, then navigate to youruploadViewController:
func fusumaImageSelected(_ image: UIImage, source: FusumaMode) { // Dismiss Fusuma first to keep the flow clean dismiss(animated: true) { // Initialize your upload view controller let uploadVC = UploadViewController() // Pass the selected image to it uploadVC.selectedImage = image // Choose your navigation style (push if in a navigation stack, or present modally) self.navigationController?.pushViewController(uploadVC, animated: true) // Or use modal presentation if needed: // self.present(uploadVC, animated: true) } }
- Don't forget to handle cancellation and permission errors:
func fusumaDismissedWithImage() { dismiss(animated: true) } func fusumaCameraRollUnauthorized() { // Show an alert to the user about photo library permission if needed let alert = UIAlertController(title: "Permission Required", message: "Please enable photo library access in Settings", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default)) present(alert, animated: true) }
To add a post-capture confirmation step, we'll subclass Fusuma's camera view controller to add a custom overlay (this avoids modifying core Fusuma source code directly, making future updates easier):
Step 1: Create a custom camera view controller
class CustomFusumaCameraVC: FusumaCameraViewController { // Custom overlay with confirm/retake buttons private lazy var confirmationOverlay: UIView = { let overlay = UIView(frame: view.bounds) overlay.backgroundColor = UIColor.black.withAlphaComponent(0.7) // Confirm button let confirmBtn = UIButton(type: .system) confirmBtn.setTitle("Confirm", for: .normal) confirmBtn.setTitleColor(.white, for: .normal) confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold) confirmBtn.addTarget(self, action: #selector(confirmPhoto), for: .touchUpInside) // Retake button let retakeBtn = UIButton(type: .system) retakeBtn.setTitle("Retake", for: .normal) retakeBtn.setTitleColor(.white, for: .normal) retakeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold) retakeBtn.addTarget(self, action: #selector(retakePhoto), for: .touchUpInside) // Layout buttons with Auto Layout overlay.addSubview(confirmBtn) overlay.addSubview(retakeBtn) confirmBtn.translatesAutoresizingMaskIntoConstraints = false retakeBtn.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ confirmBtn.bottomAnchor.constraint(equalTo: overlay.bottomAnchor, constant: -40), confirmBtn.trailingAnchor.constraint(equalTo: overlay.centerXAnchor, constant: -20), retakeBtn.bottomAnchor.constraint(equalTo: overlay.bottomAnchor, constant: -40), retakeBtn.leadingAnchor.constraint(equalTo: overlay.centerXAnchor, constant: 20) ]) overlay.isHidden = true return overlay }() override func viewDidLoad() { super.viewDidLoad() view.addSubview(confirmationOverlay) } // Override the method triggered when a photo is captured override func didTakePhoto() { super.didTakePhoto() // Show our confirmation overlay instead of returning the image immediately confirmationOverlay.isHidden = false } @objc private func confirmPhoto() { confirmationOverlay.isHidden = true // Pass the captured image back to the parent Fusuma view controller if let image = capturedImage { delegate?.fusumaCameraShot(image: image) } dismiss(animated: true) } @objc private func retakePhoto() { confirmationOverlay.isHidden = true // Restart the camera session to let the user take another photo restartCaptureSession() } }
Step 2: Use your custom camera view controller in Fusuma
When initializing Fusuma, replace its default camera view controller with your custom subclass:
let fusuma = FusumaViewController() fusuma.cameraViewController = CustomFusumaCameraVC() fusuma.delegate = self present(fusuma, animated: true)
Now, after the user takes a photo, they'll see your confirm/retake overlay. Tapping "Confirm" will send the image to the fusumaImageSelected delegate method (triggering the navigation to uploadViewController), while "Retake" will restart the camera session.
内容的提问来源于stack exchange,提问作者breath abel

