各位, 我是 Swift 的初学者,目前没有取得任何进展。 我已经使用 CollectionView 构建了一个 View ,并希望用照片库中的图片填充它。
不幸的是,我无法将 UIImages 创建为数组并在 CollectionView 中显示它们。
我可以创建一个 UIImageView 并将其相应地连接到我的 vc,并将图像加载到 View 中。
但我无法将多个图像加载到 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 选择的标签。
最佳答案
如果我理解了这个问题,那么你就差不多成功了。如果你能把问题说得更明确一点就更好了,因为我可能会浪费时间回答。
我认为你想要的是:
- 从一个空画廊开始
- 当用户选择图像时,它将添加到图库中
您的 UICollectionViewCell 实现看起来不错,所以我假设它可以工作。
尝试这些步骤。
将 dataArray 的声明更改为
var dataArray: [UIImage] = []
将 ImagePicker 委托(delegate)函数更改为:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
dataArray.append(image)
picker.dismiss(animated: true, completion: nil)
self.myCollectionView.insertItems(at: [IndexPath(item: dataArray.count, section: 0)])
}
将 cell.setData 更改为 cell.setImg
您可能需要以某种方式保存图像才能使其发挥作用。
关于ios - 如何在 CollectionView 中显示照片库或相机中选定的图像?画廊 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53546946/