我正在尝试将图像选择器中的图像添加到 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/