gpt4 book ai didi

ios - 在 Collection View 中选择和删除图像

转载 作者:行者123 更新时间:2023-11-28 14:08:16 26 4
gpt4 key购买 nike

我如何选择多张图片并打开带有共享选项的 View ,或者如何将它们从收藏 View 中删除?我对编程比较陌生,如果有人有解决方案或知道更好的方法,请回复,因为这将非常有益。谢谢。

class ViewController: UICollectionViewController, UIImagePickerControllerDelehye5gate, 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
}
}

最佳答案

在你的cellForItemAt

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

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

let imageView = cell.viewWithTag(1) as! UIImageView
imageArray.append(imageViewTwo.image!)
imageView.image = imageArray[indexPath.row]
return cell
}

你有这条线

imageArray.append(imageViewTwo.image!)

这是将额外的图像添加到数组中,您已经在 didFinishPickingMediaWithInfo 方法中将图像添加到此数组中,这会导致重复。

关于ios - 在 Collection View 中选择和删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52806369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com