ios - 将图像从 ImagePicker 添加到 CollectionView

标签 ios swift uicollectionview uiimagepickercontroller

我正在尝试将图像选择器中的图像添加到 Collection View ,然后能够根据需要添加和删除图像。

我已经添加了添加图片的功能,但不确定如何在达到限制时删除添加图片的选项。

同时从数组/ Collection View 中删除图像。

var arrayOfImages = [UIImage]()

let uploadImagesCell = UITableViewCell()

var collectionView: UICollectionView!
var cellImage = UIImageView()

@IBOutlet weak var tableView: UITableView!
var imagePicker = ImagePickerController()
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: self.view.frame.size.width / 3 - 20, height: 111)
    layout.scrollDirection = .horizontal
    collectionView = UICollectionView(frame:  CGRect(x: 0, y: 0, width: self.view.frame.width - 10, height: 150), collectionViewLayout: layout)
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.showsVerticalScrollIndicator = false
    collectionView.delegate   = self
    collectionView.dataSource = self
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.backgroundColor = UIColor.white


    self.uploadImagesCell.addSubview(collectionView)

    arrayOfImages.append(UIImage.init(named: "c")!)
}

func numberOfSections(in collectionView: UICollectionView) -> Int {

    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return arrayOfImages.count
}
var path: Int!

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
    self.cellImage = UIImageView(frame: CGRect(x: 0, y: 0, width: cell.frame.size.width, height: cell.frame.size.height))
    cell.backgroundColor = UIColor.blue

    cellImage.image = arrayOfImages[indexPath.row]

    cell.addSubview(cellImage)


    return cell
}
func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}



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

    print("test")


        var configuration = Configuration()
            configuration.doneButtonTitle = "Finish"
            configuration.noImagesTitle = "Sorry! There are no images here!"
            configuration.recordLocation = false

            imagePicker = ImagePickerController(configuration: configuration)
            imagePicker.imageLimit = 1
            imagePicker.delegate = self

            present(imagePicker, animated: true, completion: nil)
}

func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
    imagePicker.dismiss(animated: true, completion: nil)
}
public var imageAssets: [UIImage] {
    return AssetManager.resolveAssets(imagePicker.stack.assets)
}
func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
    print("cancel")
}

func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {


    let images = imageAssets[0]


    self.arrayOfImages.insert(images, at: 0)


    self.collectionView.reloadData()

    imagePicker.dismiss(animated: true, completion: nil)
}

最佳答案

在单元格内添加一个按钮,并为该按钮使用委托(delegate)或回调。

import UIKit

class AttachCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var attachImageView: UIImageView!

    var delete_callback : ((_ identi : Int) -> ())?

    override func awakeFromNib() {
        super.awakeFromNib()
    }
    @IBAction func deleteBtn(_ sender: Any) {
        delete_callback?(self.tag)
    }
}

在 collectionview delegate 中添加回调,如下所示。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let identifier = "AttachCollectionViewCell"
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
        if let attachCell = cell as? AttachCollectionViewCell {
            attachCell.delete_callback = { (ident) -> Void in
                self.pickedImages.remove(at: ident)
                self.attachCollectionView.reloadData()
            }
        }
        return cell
    }

关于ios - 将图像从 ImagePicker 添加到 CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042527/

相关文章:

ios - 如何从 imagePickerController 执行推送 segue?

ios - 如何将从相机拍摄的图像保存到 Parse?

ios - iOS上的FFT/吉他调谐器

ios - 在 iOS 中更改 toast 的字体、字体大小

ios - 来自 Apple 网站的日期示例代码不起作用?

ios - 使用 Swift 从库中获取所选视频的当前时间

ios - SetDelegate 导致 'unrecognized selector' 异常

swift - 如何在 Swift 的 UICollectionViewCell 中将图像显示到中心

ios - 如何在不使用阴影路径和添加图层的情况下仅向 UIView 的顶部添加阴影

ios - 在同一个索引路径中添加两个单元格?