无法将类型 'String' 的值转换为预期的参数类型 'UIImage'。 UIImagePicker 放入collectionView - 想详细查看VC?

Can't convert value of type 'String' to expected argument type 'UIImage'. UIImagePicker puts into collectionView - want to view in a detailed VC?

我正在尝试通过 UIImagePicker 创建相册到 CollectionView 中,但无法在详细的 UIViewController 中再次转到同一张照片。扯我的头发,这只是一个教程,因为我刚刚开始编码!

谁能告诉我哪里做错了?

这是我的 ViewController:

   import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate {
    @IBOutlet private weak var addButton: UIBarButtonItem!
    @IBOutlet private weak var collectionView:UICollectionView!
    @IBOutlet private weak var trashButton:UIBarButtonItem!

    var testItems = [Person]()

    var stormTrooperCollectionArray: [UIImage] = [#imageLiteral(resourceName: "StormTrooper-3052-423a-8c57-7220081e1585_800x"), #imageLiteral(resourceName: "ST3"), #imageLiteral(resourceName: "ST2"), #imageLiteral(resourceName: "ST4")]

    @IBAction func addItem() {
        addNewPerson()
    }

    @IBAction func trashItem(_ sender: Any) {
        if let selectedItems = collectionView.indexPathsForSelectedItems {
            let itemsForDeletion = selectedItems.map{[=13=].item}.sorted().reversed()
            for item in itemsForDeletion {
                testItems.remove(at: item)
            }
            collectionView.deleteItems(at: selectedItems)
        }
    }

    @objc func refresh() {
        addItem()
        collectionView.refreshControl?.endRefreshing()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Set up a 3-column Collection View
        let width = (view.frame.size.width - 20) / 3
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width:width, height:width)
        // Refresh Control
        let refresh = UIRefreshControl()
        refresh.addTarget(self, action: #selector(self.refresh), for: UIControlEvents.valueChanged)
        collectionView.refreshControl = refresh
        // Edit
        navigationItem.leftBarButtonItem = editButtonItem
        navigationController?.isToolbarHidden = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "DetailSegue" {
            if let dest = segue.destination as? DetailsViewController,
                let index = sender as? IndexPath {
                dest.detailedImageHi = stormTrooperCollectionArray [index.row]
            }
        }
    }

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        navigationController?.isToolbarHidden = !editing
        addButton.isEnabled = !editing
        trashButton.isEnabled = editing
        collectionView.allowsMultipleSelection = editing
        let indexes = collectionView.indexPathsForVisibleItems
        for index in indexes {
            let cell = collectionView.cellForItem(at: index) as! CollectionViewCell
            cell.isEditing = editing
        }
    }
}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return stormTrooperCollectionArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
        //cell.titleLabel.text = stormTrooperCollectionArray[indexPath.row]

        cell.selectionImage.image = stormTrooperCollectionArray[indexPath.row]
        cell.isEditing = isEditing
        return cell
    }

    @objc func addNewPerson() {
        let picker = UIImagePickerController()
        picker.allowsEditing = true
        picker.delegate = self
        present(picker, animated: true)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else { return }

        let imageName = UUID().uuidString
        let imagePicture = getDocumentsDirectory()

        if let jpegData = UIImageJPEGRepresentation(image, 80) {
            try? jpegData.write(to: imagePicture)
        }

        //let detailedItem = Person(imageHi: imageName)
        //testItems.append(detailedItem)

        let detailedItem = Person(imageHi: imageName)
//KEEPS THROWING AN ERROR HERE WHICH SAYS: Cannot convert value of type 'String' to expected argument type 'UIImage'
        stormTrooperCollectionArray.append(detailedItem)
        collectionView?.reloadData()

        dismiss(animated: true)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if !isEditing {
            performSegue(withIdentifier: "DetailSegue", sender: indexPath)
        }
    }

    func getDocumentsDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentsDirectory = paths[0]
        return documentsDirectory
    }
}

这是我的 VC CollectionView 中的单元格:

import UIKit

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var selectionImage: UIImageView!

}

这是我的 VC 详细视图控制器:

import UIKit

class DetailsViewController: UIViewController {

    var selection: String!
    var detailedImageHi: UIImage!


    @IBOutlet private weak var detailsLabel: UILabel!
    @IBOutlet private weak var detailedImage: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        detailsLabel.text = selection
        detailedImage.image = detailedImageHi
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

这是我的 VC NSObject Swift 文件:

import UIKit

class Person: NSObject {

    var imageHi: UIImage

    init(imageHi: UIImage){
        self.imageHi = imageHi
    }
}

在您的代码中,UIImage 是 image 而不是 imageName:

guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else { return }

所以你所要做的就是将正确的参数传递给 Person 的构造函数:

let detailedItem = Person(imageHi: image)

imageName 是通过 UUID().uuidString 生成的随机字符串,在此代码中用于以随机且唯一的名称将图像保存到文档目录。