everybody, I'm a beginner in Swift and I'm not making any progress at the moment. I have built a view with CollectionView and would like to fill it with pictures from the Photo Library.
Unfortunately I can't create the UIImages as an array and display them in CollectionView.
I can create a single UIImageView and connect it to my vc accordingly and also load an image into the view.
But I don't get it to load several images into the CollectionView.
How can I build my own gallery in which I load pictures from the camera or photo library?
That's my previous code
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var myCollectionView: UICollectionView!
let dataArray = ["1", "2", "3", "4", "5"]
override func viewDidLoad() {
super.viewDidLoad()
// Set Delegates
self.myCollectionView.delegate = self
self.myCollectionView.dataSource = self
}
@IBAction func chooseImage(_ sender: UIBarButtonItem) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Quelle", message: "Wähle eine Quelle aus", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Kamera", style: .default, handler: { (action:UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera){
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
} else {
print("Kamera nicht verfügbar")
}
}))
actionSheet.addAction(UIAlertAction(title: "Foto", style: .default, handler: { (action:UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Abbrechen", style: .cancel, 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
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath) as! ItemCell
cell.setData(text: self.dataArray[indexPath.row])
return cell
}
The cell is implemented as follows.
class ItemCell: UICollectionViewCell {
@IBOutlet weak var textLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setData(text: String) {
self.textLabel.text = text
}
func setImg(image: UIImage) {
self.imageView.image = image
}
But now I want to get UIImages instead of the labels "1", "2" and so on and only those I choose myself with the imagePickerController.