You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Fusuma代码实现选图/拍照后跳转至新ViewController?

Absolutely! You can achieve both requirements with minimal modifications to Fusuma. Let's break this down into two clear, actionable steps:

1. Navigate to uploadViewController after selecting/capturing a photo

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 your uploadViewController:
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)
}
2. Add a confirm/retake view after capturing a photo

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:33:23