gpt4 book ai didi

ios - 如何在数组 "Cannot convert value of type ' UIImageView 中附加 UIImage!”

转载 作者:行者123 更新时间:2023-11-28 11:55:38 26 4
gpt4 key购买 nike

我想将 UIImage 附加到我的数组中,我已经创建了一个类和函数来将所有数据附加到我的数组中,但执行了 UIImage,我已经尝试过但错误提示 “无法转换类型的值'UIImageView!" 到预期的参数类型 'UIImage'"。

我对 swift 还是很陌生,无法找出这个错误的解决方案。

非常感谢你们! :)

下面是我的代码:

类:

import Foundation
import UIKit

class Customer {
var name: String
var lastName: String
var contactNumber: Int
var contactImage: UIImage

var fullName: String {
return "\(name + " " + lastName)"
}

init(name: String, lastName: String, contactNumber: Int, contactImage: UIImage) {
self.name = name
self.lastName = lastName
self.contactNumber = contactNumber
self.contactImage = contactImage
}
}

var customer = [Customer]()

添加到数组中的函数

 import Foundation
import UIKit

class AddCustomer {
func addCustomer(name: String, lastName: String, fullName: String, contact: Int, contactImg: UIImage){

let newCustomer = Customer(name: name, lastName: lastName, contactNumber: contact, contactImage: contactImg)

if name != "" && lastName != ""{
customer.append(newCustomer)
} else {
print("Cannot Proceed")
}
print(customer.count)
}
}

现在这是我的 MainViewController:

import UIKit

class MainVC: UIViewController {

var vmCustomer = AddCustomer()
var contactNum = Int()

@IBOutlet weak var nameText: UITextField!
@IBOutlet weak var lastNameText: UITextField!
@IBOutlet weak var contactNumber: UITextField!
@IBOutlet weak var fullNameLbl: UILabel!
@IBOutlet weak var imageUpload: UIImageView!

@IBAction func addImage(_ sender: UIButton) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self

let imageAction = UIAlertController(title: "Photo Source", message: "Choose a Source", preferredStyle: .actionSheet)

imageAction.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action: UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
self.present(imagePicker, animated: true, completion: nil)
} else {
print("Camera Not Available")
}
}))

imageAction.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action: UIAlertAction) in
imagePicker.sourceType = .photoLibrary
self.present(imagePicker, animated: true, completion: nil)

}))

imageAction.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction) in

}))

self.present(imageAction, animated: true, completion: nil)
}

@IBAction func submitButton(_ sender: UIButton) {

guard let name = nameText.text else {return}
guard let lastName = lastNameText.text else {return}
guard let fullName = fullNameLbl.text else {return}
guard let contactText = contactNumber.text else {return}

if contactText != "" {
contactNum = Int(contactText)!
} else {
print("Error")
}

vmCustomer.addCustomer(name: name, lastName: lastName, fullName: fullName, contact: contactNum, contactImg: imageUpload)
fullNameLbl.text = "\(name) \(lastName)"
}

override func viewDidLoad() {
super.viewDidLoad()
contactNumber.keyboardType = .numberPad
}
}

扩展 MainVC:UIImagePickerControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageUpload.image = image
picker.dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}

最佳答案

您正在尝试将 UIImageView 用作 UIImage

UIImageView 是一个显示图像的 View ,如果你想获得呈现的图像,那么使用

guard let image = imageUpload.image else { return }

然后使用该图像在 submitButton 函数中添加到客户列表而不是 imageUpload

vmCustomer.addCustomer(name: name, lastName: lastName, fullName: fullName, contact: contactNum, contactImg: image)

关于ios - 如何在数组 "Cannot convert value of type ' UIImageView 中附加 UIImage!”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51122995/

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