简体   繁体   English

如何在CollectionView中显示照片库或相机中的选定图像? 图库视图

[英]How can I display selected images from the Photo Library or Camera in a CollectionView? Gallery view

everybody, I'm a beginner in Swift and I'm not making any progress at the moment. 大家好,我是Swift的初学者,目前我没有任何进展。 I have built a view with CollectionView and would like to fill it with pictures from the Photo Library. 我已经用CollectionView构建了一个视图,并希望用照片库中的图片填充它。

Unfortunately I can't create the UIImages as an array and display them in CollectionView. 不幸的是,我无法将UIImages创建为数组并将其显示在CollectionView中。

I can create a single UIImageView and connect it to my vc accordingly and also load an image into the view. 我可以创建一个UIImageView并将其相应地连接到我的vc,还将图像加载到视图中。

But I don't get it to load several images into the CollectionView. 但是我不知道如何将几个图像加载到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. 但是现在我想获取UIImages而不是标签“ 1”,“ 2”,依此类推,只有那些我使用imagePickerController选择自己的标签。

If I understand the question, you're nearly there. 如果我理解这个问题,您就快到了。 It would be better if you could be a little more explicit with the question, as I may be wasting my time with the answer. 如果您对问题更明确一点会更好,因为我可能会浪费时间回答问题。

What I think you want is: 我认为您想要的是:

  • Start with an empty gallery 从一个空的画廊开始
  • When the user selects an image, it is added to the gallery 当用户选择图像时,图像将被添加到图库中

Your UICollectionViewCell implementation looks ok, so I'll assume it will work. 您的UICollectionViewCell实现看起来还不错,所以我假设它可以工作。

Try these steps. 尝试这些步骤。

Change the declaration of dataArray to 将dataArray的声明更改为

var dataArray: [UIImage] = []

Change the ImagePicker delegate function to: 将ImagePicker委托函数更改为:

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)])
}

Change cell.setData to cell.setImg 将cell.setData更改为cell.setImg

You may need to work on persisting the images in some way to make this useful. 您可能需要以某种方式对图像进行持久化以使其有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示从图库到表视图中选择的照片 - Display Photo Selected From Gallery into Table View 如何在收藏夹视图中显示从图库中选择的多个图像? - How to display multiple images selected from gallery in collection view? 如何显示照片库中刚选择的图像? - How to display images that in photo gallery that were just selected? 如何在uiwebview ios swift上显示照片库中的图像? - How to display images from Photo gallery on uiwebview ios swift? 如何避免从照片库中选择或从ios中的相机中捕获的图像的背景? - how to avoid background for an image which is selected from photo gallery or captured from Camera in ios? 如何将滤镜应用于照片库中的选定图像,而不仅仅是硬编码的图像 - How do I go about applying filters to selected images from the photo library not just hardcoded ones 如何将纯色图像插入照片库集合视图 - How to insert solid color images into photo library collectionview 我确实从相机或图库中选择图像以及如何保存该图像以显示在其他视图中? - i did Choose an image from camera or gallery and how to save that image to display in other View? 如何使用更少的内存在 collectionView 中显示照片库 - How to display photo library in collectionView using less memory 在表格视图中显示从相机拍摄的照片 - Display a photo taken from camera in a table view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM