如何将从Wikimedia API获取的图片URL传递到另一ViewController并显示
Hey there! Let's figure out why your Wikimedia API image URL isn't showing up in your second view controller. The core issue here is how you're passing the image data and setting it up—let's walk through the fixes step by step.
What's Wrong with Your Current Setup
Right now, your target DefinitionViewController is trying to receive a UIImageView instance (imageIllustrative), but you're not actually passing that value from your source view controller. Plus, it's better practice to pass the image URL string (the raw data you get from the API) instead of a pre-loaded image view—this lets the target controller handle loading the image on its own, which is more flexible.
Step 1: Update the Target View Controller's Variables
First, modify your DefinitionViewController to store the image URL string instead of a UIImageView:
import SwiftyJSON import Alamofire import SDWebImage class DefinitionViewController: UIViewController { @IBOutlet weak var definitionIllustrationImageView: UIImageView! @IBOutlet weak var definitionTextView: UITextView! // Replace the UIImageView variable with a string to hold the URL var definition: String? var imageURLString: String? override func viewDidLoad() { super.viewDidLoad() // Set the definition text (with a fallback if nil) definitionTextView.text = definition ?? "Désolé,ce que vous cherchez ne se trouve pas sur Wikipedia" // Load the image using SDWebImage if let urlString = imageURLString, let imageURL = URL(string: urlString) { // Optional: Add a placeholder image for better UX definitionIllustrationImageView.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "placeholder-image")) } else { // Handle cases where there's no image URL definitionIllustrationImageView.image = UIImage(named: "no-image-available") // Or hide the image view: definitionIllustrationImageView.isHidden = true } } }
Step 2: Pass Data via prepare(for:sender:)
You're calling performSegue but not actually passing the imageSource and termDefinitionInfo to the target controller. Add this method to your source view controller to handle data transfer:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Make sure we're handling the correct segue guard segue.identifier == "toDefinition", let destinationVC = segue.destination as? DefinitionViewController else { return } // Pass the definition text destinationVC.definition = self.termDefinitionInfo // Pass the image URL string from your API response destinationVC.imageURLString = imageSource }
Step 3: Double-Check Your Setup
- Verify that your segue's identifier (
toDefinition) matches exactly in both storyboard and code. - Ensure
definitionIllustrationImageViewis properly connected to your storyboard UIImageView (no broken IBOutlet connections). - Confirm that
imageSourceis not empty when you print it—if it's nil, you'll need to debug your API response parsing to make sure you're grabbing the correct "source" value from the JSON.
That's it! With these changes, your target controller will receive the image URL and use SDWebImage to load and display the image correctly.
内容的提问来源于stack exchange,提问作者LEKYSMA

