Saad Sikandar
Saad Sikandar

Reputation: 45

How to append UIImage in array "Cannot convert value of type 'UIImageView!"

I want to append the UIImage to my array, I have already create a class and function to append all the data to my array but execpt the UIImage, i have tried but it error says "Cannot convert value of type 'UIImageView!" to expected argument type 'UIImage'".

I am still very new in swift and can't figure out the solution of this error.

Thank you very much Guys! :)

Below is my code:

Class:

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]()

Function to append in the Array

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

now here is my 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
 }
}

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

Upvotes: 1

Views: 2674

Answers (1)

zombie
zombie

Reputation: 5259

You are trying to use UIImageView as a UIImage

UIImageView is a view to display the image and if you want to get the presented image then use

guard let image = imageUpload.image else { return }

and then use that image to add to the customer list in submitButton function instead of imageUpload

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

Upvotes: 2

Related Questions