swift 5.1 将来自 UIImagePickerController 的图像添加到一个数组,该数组被添加到一个 UICollectionView
swift 5.1 Adding images from UIImagePickerController to an array that gets added to a UICollectionView
到目前为止,这是我的代码。当我点击库中的图像时,它附加到数组,但 collectionView 不显示图像也不添加到 collectionView。
@IBOutlet weak var collectionView: UICollectionView!
let imagePicker = UIImagePickerController()
// Array that handles the images to the collectionView.
var photoArray: [UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
imagePicker.delegate = self
}
// Button that requests the UIImagePicker
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true)
}
// MARK: - UIPickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo
info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
photoArray.append(pickedImage)
}
}
// MARK: - collectionView DataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)
-> Int {
return photoArray.count
}
将数组添加到 collectionView
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)
-> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CELL",
for: indexPath) as! PhotosCell
cell.photoImageView.image = photoArray[indexPath.row]
return cell
}
选择图像后重新加载 collectionView
。
func imagePickerController(
_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let pickedImage = info[.originalImage] as? UIImage {
photoArray.append(pickedImage)
collectionView.reloadData()
}
}
到目前为止,这是我的代码。当我点击库中的图像时,它附加到数组,但 collectionView 不显示图像也不添加到 collectionView。
@IBOutlet weak var collectionView: UICollectionView!
let imagePicker = UIImagePickerController()
// Array that handles the images to the collectionView.
var photoArray: [UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
imagePicker.delegate = self
}
// Button that requests the UIImagePicker
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true)
}
// MARK: - UIPickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo
info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
photoArray.append(pickedImage)
}
}
// MARK: - collectionView DataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)
-> Int {
return photoArray.count
}
将数组添加到 collectionView
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)
-> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CELL",
for: indexPath) as! PhotosCell
cell.photoImageView.image = photoArray[indexPath.row]
return cell
}
选择图像后重新加载 collectionView
。
func imagePickerController(
_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let pickedImage = info[.originalImage] as? UIImage {
photoArray.append(pickedImage)
collectionView.reloadData()
}
}