все, я новичок в Swift и сейчас не делаю никаких успехов.Я построил представление с CollectionView и хотел бы заполнить его изображениями из библиотеки фотографий.
К сожалению, я не могу создать UIImages в виде массива и отобразить их в CollectionView.
Я могу создать один UIImageView и соответственно подключить его к моему виртуальному каналу, а также загрузить изображение в представление.
Но я не могу загрузить несколько изображений в CollectionView.
Как мне создать собственную галерею, в которую я загружаю изображения с камеры или библиотеки фотографий?
Это мой предыдущий код
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
}
Ячейка реализована следующим образом.
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
}
Но теперь я хочу получить UIImages вместо меток «1», «2» и т. Д. И только те, которые я выбираю сам с помощью imagePickerController.