在 Collection 视图中删除多个图像
Deleting Multiple Images in a Collection View
如何从我的 collection 图片视图中删除图片?其次,我如何 select 多张图片并调出带有共享选项的视图,或者如何从 collection 视图中删除它们?我对编程比较陌生,如果有人有解决方案或知道更好的方法,请回复,因为这将非常有益。谢谢。
class ViewController: UICollectionViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var imageViewTwo: UIImageView!
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
imageViewTwo.isHidden = true
}
@IBAction func chooseImage(_ sender: UIBarButtonItem) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a Source", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action: UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
} else {
print("Camera is not available.")
}
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action: UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
imageViewTwo.image = image
imageArray.append(imageViewTwo.image!)
collectionView.reloadData()
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width / 3 - 1
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
var itemsSelected = [IndexPath]()
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.allowsMultipleSelection = true
if !(itemsSelected.contains(indexPath)) {
itemsSelected.append(indexPath)
}
}
@IBAction func deletePressed(_ sender: UIBarButtonItem) {
itemsSelected.forEach {
imageArray.remove(at:[=11=].row)
}
collectionView.deleteItems(at:itemsSelected)
itemsSelected.removeAll()
}
}
删除1张图片
let index = 0 // or any number < imageArray.count
imageArray.remove(at:index)
collectionView.deleteItems(at: [IndexPath(row: index, section: 0)])
对于多行删除,您需要先启用
collectionView.allowsMultipleSelection = true
并创建一个数组来保存选定的行,例如
var itemsSelected = [IndexPath]()
当用户点击 didSelectItemAt
时执行
if !(itemsSelected.contains(indexPath)) {
itemsSelected.append(indexPath)
}
当你想删除它们时
itemsSelected.forEach {
imageArray.remove(at:[=14=].row)
}
collectionView.deleteItems(at:itemsSelected)
itemsSelected.removeAll()
如何从我的 collection 图片视图中删除图片?其次,我如何 select 多张图片并调出带有共享选项的视图,或者如何从 collection 视图中删除它们?我对编程比较陌生,如果有人有解决方案或知道更好的方法,请回复,因为这将非常有益。谢谢。
class ViewController: UICollectionViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var imageViewTwo: UIImageView!
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
imageViewTwo.isHidden = true
}
@IBAction func chooseImage(_ sender: UIBarButtonItem) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a Source", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action: UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
} else {
print("Camera is not available.")
}
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action: UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
imageViewTwo.image = image
imageArray.append(imageViewTwo.image!)
collectionView.reloadData()
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width / 3 - 1
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
var itemsSelected = [IndexPath]()
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.allowsMultipleSelection = true
if !(itemsSelected.contains(indexPath)) {
itemsSelected.append(indexPath)
}
}
@IBAction func deletePressed(_ sender: UIBarButtonItem) {
itemsSelected.forEach {
imageArray.remove(at:[=11=].row)
}
collectionView.deleteItems(at:itemsSelected)
itemsSelected.removeAll()
}
}
删除1张图片
let index = 0 // or any number < imageArray.count
imageArray.remove(at:index)
collectionView.deleteItems(at: [IndexPath(row: index, section: 0)])
对于多行删除,您需要先启用
collectionView.allowsMultipleSelection = true
并创建一个数组来保存选定的行,例如
var itemsSelected = [IndexPath]()
当用户点击 didSelectItemAt
时执行
if !(itemsSelected.contains(indexPath)) {
itemsSelected.append(indexPath)
}
当你想删除它们时
itemsSelected.forEach {
imageArray.remove(at:[=14=].row)
}
collectionView.deleteItems(at:itemsSelected)
itemsSelected.removeAll()