Пока это мой код. Когда я нажимаю на изображения в библиотеке, они добавляются в массив, но 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
}