【发布时间】:2018-11-29 20:20:29
【问题描述】:
大家, 我是 Swift 的初学者,目前没有任何进展。 我已经用 CollectionView 构建了一个视图,并希望用照片库中的图片填充它。
不幸的是,我无法将 UIImages 创建为数组并将它们显示在 CollectionView 中。
我可以创建一个 UIImageView 并将其连接到我的 vc 中,并将图像加载到视图中。
但我无法将多张图片加载到 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 选择的那些。
【问题讨论】:
-
Select multiple image from photo library using swift 可能重复您不能使用 imagePicker 选择多个图像,但只能选择一个
标签: ios swift uicollectionview uiimageview uiimage