首页 > 解决方案 > 将数据从一个视图控制器传递到另一个(图像)

问题描述

错误:

线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)

目的地控制器

var getImage = UIImage()
var name = String()
var gender = String()
var house = String()
var ancestry = String()

override func viewDidLoad() {
    super.viewDidLoad()
    imageView.image = (charData.image)as! UIImage
    nameLabel.text! = name
    houseLabel.text! = house

    // Do any additional setup after loading the view.
}

源控制器

var charactersData = [Character]()

override func viewDidLoad() {
    super.viewDidLoad()
    loadData()
}

func loadData()
{
    DispatchQueue.main.async {
        Alamofire.request("http://hp-api.herokuapp.com/api/characters").responseJSON(completionHandler: {
            (response) in
            switch response.result
            {
            case.success(let value):
                let json = JSON(value)
                print(json)
                json.array?.forEach({
                    (character) in
                    let character = Character(name: character["name"].stringValue, house:character["house"].stringValue,image:character["image"].stringValue, gender: character["gender"].stringValue, ancestry: character["ancestry"].stringValue)
                    self.charactersData.append(character)
                })
                self.tableView.reloadData()
            case.failure(let error):
                print(error.localizedDescription)
            }
        })
    }
}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return charactersData.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CharTableViewCell

    cell.nameLabel.text = "Name: " +  charactersData[indexPath.row].name
    cell.houseLabel.text = "House: " + charactersData[indexPath.row].house

    if let imageURL = URL(string: self.charactersData[indexPath.row].image) {
        DispatchQueue.global().async {
            let data = try? Data(contentsOf: imageURL)
            if let data = data {
                let image = UIImage(data: data)
                DispatchQueue.main.async {
                    cell.charImageView.image = image
                }
            }
        }
    }
    return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let hpc = storyboard?.instantiateViewController(withIdentifier: "CharDetails") as? CharDetailsViewController
    hpc?.getImage =  (charactersData[indexPath.row].image) as! UIImage
    hpc?.name = charactersData[indexPath.row].name
    hpc?.house = charactersData[indexPath.row].house
    self.navigationController?.pushViewController(hpc!, animated: true)
}

我试图将图像传递给另一个控制器,但似乎我得到了那个错误,有人可以帮助我。除了图像之外,所有其他数据(例如名称和房屋)都在正确传递。请让我知道在哪里进行更改

标签: swift

解决方案


这可能有效:

override func viewDidLoad() {
    super.viewDidLoad()

    imageView.image = getimage   // change this in view did load method
}

推荐阅读