我想将UIImage附加到我的数组中,我已经创建了一个类和函数来将所有数据附加到我的数组中,但是执行了UIImage,但是我尝试了但错误提示”“无法转换类型的值'UIImageView!” 为预期的参数类型“ UIImage””。
我仍然是个新手,无法解决此错误。
非常感谢你们! :)
下面是我的代码:
班级:
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)
}
}
答案 0 :(得分:2)
您正在尝试将UIImageView
用作UIImage
UIImageView
是显示图像的视图,如果要获取显示的图像,请使用
guard let image = imageUpload.image else { return }
,然后使用该图像将其添加到submitButton
函数而不是imageUpload
的客户列表中
vmCustomer.addCustomer(name: name, lastName: lastName, fullName: fullName, contact: contactNum, contactImg: image)