将图像从ImagePicker添加到CollectionView

时间:2018-06-26 12:06:13

标签: ios swift uicollectionview uiimagepickercontroller

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

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

还要从阵列/集合视图中删除图像。

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

3 个答案:

答案 0 :(得分:1)

在单元格内添加一个按钮,并对按钮使用委托或回调。

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委托内部添加回叫,如下所示。

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
    }

答案 1 :(得分:0)

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if imageAssets.count==photoLimit 
    { //display alert  you have reached the limit}
    else{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)
}
}

答案 2 :(得分:0)

设置单元格右上角的减号按钮。

这在您的代码中

    cell.cancelBtn = {
            self. arrayOfImages.remove(at: indexPath.item - 1)
            collectionView.reloadData()
        }

这将从集合视图以及数组中删除图像。

相关问题