ios - 将 ImagePicker 中的图像添加到 CollectionView
问题描述
我正在尝试将图像从我的图像选择器添加到集合视图,然后能够根据需要添加和删除图像。
我添加了添加图像的功能,但不确定如何在达到限制时删除添加图像的选项。
还从数组/集合视图中删除图像。
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)
}
解决方案
在单元格内添加一个按钮,并为按钮使用委托或回调。
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
}
推荐阅读
- excel - 从excel读取多组行并将其放在不同的文件中
- bash - 在 Bash 中将多行字符串转换为单个逗号分隔的列表
- javascript - 停止子 DIV 以从其父页面继承样式
- javascript - Django模板中的Javascript中的“未捕获的语法错误:意外的标识符”
- ruby-on-rails - 无法在 docker 中启动 sphinxsearch
- javascript - 我需要传递 a 预期的参数。标记文件,使用 javascript
- amazon-web-services - AmazonS3Encryption 忽略提供的 EncryptionMaterials
- rxjs - 根据项目值有条件地产生多个值并将其合并到原始流中
- ios - 需要 CoreData 属性 id
- visual-studio-code - 如何在 VScode 中删除带有某些关键字的行