gpt4 book ai didi

swift - 将数据从一个 View Controller 传递到另一个(图片)

转载 作者:行者123 更新时间:2023-11-28 11:47:53 25 4
gpt4 key购买 nike

错误:

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

目标 Controller

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.
}

源 Controller

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)
}

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

最佳答案

这可能有效:

override func viewDidLoad() {
super.viewDidLoad()

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

关于swift - 将数据从一个 View Controller 传递到另一个(图片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com