Swift 如何从UICollectionView中选择多个图像并将其传输到另一个视图控制器?

Swift 如何从UICollectionView中选择多个图像并将其传输到另一个视图控制器?,swift,uiimageview,uicollectionview,uiimage,data-transfer,Swift,Uiimageview,Uicollectionview,Uiimage,Data Transfer,代码是用Swift写的。我正在构建一个社交应用程序,用户可以在其中发帖。我使用Firebase作为后端(数据库、存储)。因此,我有一个UICollectionView,它从设备的照片库获取所有照片,并使用自定义单元格填充收集视图。在同一个视图控制器中,我有另一个自定义单元格,用户可以使用它来拍照和发表文章。更清楚地说: 如果用户决定拍摄照片,当他们单击“使用照片”时,他们需要显示给新的视图控制器,该控制器应显示他们刚刚拍摄的照片以及其他选项(例如标题、说明和标签,使用UITextFields&

代码是用Swift写的。我正在构建一个社交应用程序,用户可以在其中发帖。我使用Firebase作为后端(数据库、存储)。因此,我有一个
UICollectionView
,它从设备的照片库获取所有照片,并使用自定义单元格填充收集视图。在同一个视图控制器中,我有另一个自定义单元格,用户可以使用它来拍照和发表文章。更清楚地说:

  • 如果用户决定拍摄照片,当他们单击“使用照片”时,他们需要显示给新的视图控制器,该控制器应显示他们刚刚拍摄的照片以及其他选项(例如标题、说明和标签,使用
    UITextFields
    &
    UITextView

  • 如果用户决定从他们自己的库中选择多张照片,我必须以某种方式标记这些照片/单元格(即使用按钮进行复选标记),将所选照片添加到阵列中(有一些限制,顶部可能有10张照片)。当他们单击“下一步”按钮时,需要将阵列发送到新的Post View控制器,在该控制器中,所有图像都应该动态显示,可能使用水平
    UICollectionView
    (?!)(如果意外选择图像,可以选择删除图像),并且如上所述,现在有机会添加标题、说明等,我不知道该怎么做

我一直在寻找解决方案,但这几天我一直在努力,所以非常欢迎帮助

以下是我在Collection View controller中拥有的功能(注:我没有包括具有从照片中获取图像功能的部分)

UICollectionView
的屏幕截图如下所示:

这是我在照片库单元格中的代码:

import UIKit

class PrePhotoPostPhotoLIbraryCell: UICollectionViewCell {

    // MARK: Outlets
    @IBOutlet weak var photoLibraryImage: UIImageView!

    // var selectedPhotos = [UIImageView]()

    @IBAction func selectedButtonPressed(_ sender: UIButton) {
        self.layer.borderWidth = 3.0
        self.layer.borderColor = isSelected ? UIColor.blue.cgColor : UIColor.clear.cgColor
    }
    override func awakeFromNib() {

        photoLibraryImage.clipsToBounds = true
        photoLibraryImage.contentMode = .scaleAspectFill
        photoLibraryImage.layer.borderColor = UIColor.clear.cgColor
        photoLibraryImage.layer.borderWidth = 1
        photoLibraryImage.layer.cornerRadius = 5

    }
}

首先声明一个可变类型的数组,该数组将在其中存储选定的单元格项

var _selectedCells : NSMutableArray = []
然后在视图加载函数中添加以下代码

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //this will allow multiple selection on uicollectionviewcell
        CollectionView.allowsMultipleSelection=true //CollectionView is your CollectionView outlet
}
然后,实现collectionview的委托功能,用于选择和取消选择单元格

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){

        //add the selected cell contents to _selectedCells arr when cell is selected
        _selectedCells.add(indexPath)
        collectionView.reloadItems(at: [indexPath])
    }

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

        //remove the selected cell contents from _selectedCells arr when cell is De-Selected

        _selectedCells.remove(indexPath)
        collectionView.reloadItems(at: [indexPath])
    }
我建议将所选项的nsindepath保存在一个数组中,然后在委托函数
cellForItemAt indepath
中使用它作为比较的基础

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YOUR_CELL_Identifier", for: indexPath as IndexPath)

//add your tick mark image to the cell in your storyboard or xib file.
let tickImage = cell.viewWithTag(YOUR_IMAGE_TAG_HERE) as? UIImageView

//Show tickImage if the cell is selected and hide tickImage if cell is NotSelected/deSelected.or whatever action you want to perform in case of selection and deselection of cell.

if _selectedCells.contains(indexPath) {
cell.isSelected=true
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.top)
tickImage?.isHidden=false
}
else{
cell.isSelected=false
tickImage?.isHidden=true
}
return cell
}

为了将项目发送到下一个控制器,请从选定的indexpaths获取所有项目。

这就是我要找的!非常感谢。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YOUR_CELL_Identifier", for: indexPath as IndexPath)

//add your tick mark image to the cell in your storyboard or xib file.
let tickImage = cell.viewWithTag(YOUR_IMAGE_TAG_HERE) as? UIImageView

//Show tickImage if the cell is selected and hide tickImage if cell is NotSelected/deSelected.or whatever action you want to perform in case of selection and deselection of cell.

if _selectedCells.contains(indexPath) {
cell.isSelected=true
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.top)
tickImage?.isHidden=false
}
else{
cell.isSelected=false
tickImage?.isHidden=true
}
return cell
}