簡體   English   中英

如何在CollectionView中顯示照片庫或相機中的選定圖像? 圖庫視圖

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

大家好,我是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選擇自己的標簽。

如果我理解這個問題,您就快到了。 如果您對問題更明確一點會更好,因為我可能會浪費時間回答問題。

我認為您想要的是:

  • 從一個空的畫廊開始
  • 當用戶選擇圖像時,圖像將被添加到圖庫中

您的UICollectionViewCell實現看起來還不錯,所以我假設它可以工作。

嘗試這些步驟。

將dataArray的聲明更改為

var dataArray: [UIImage] = []

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

將cell.setData更改為cell.setImg

您可能需要以某種方式對圖像進行持久化以使其有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM