【问题标题】:How to append UIImage in array "Cannot convert value of type 'UIImageView!"如何将 UIImage 附加到数组“无法转换 'UIImageView 类型的值!”
【发布时间】:2018-07-01 11:47:02
【问题描述】:

我想将 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)
 }
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    您正在尝试将UIImageView 用作UIImage

    UIImageView 是一个显示图像的视图,如果您想获得呈现的图像,请使用

    guard let image = imageUpload.image else { return }
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      相关资源
      最近更新 更多