简体   繁体   English

尝试学习如何将照片从照片库加载到收藏夹视图

[英]trying to learn how to load images from photo library to collection view

I am trying to learn how to use collections views and what I want to do is be able to access the photos in the photo library and put them in a collection view . 我正在尝试学习如何使用收藏夹视图,而我想要做的是能够访问照片库中的照片并将它们放入收藏夹视图中。

The problem i'm having is getting the photos to show up in the cell when I click them nothing shoes up in the cell. 我遇到的问题是,当我单击它们时,照片无法显示在单元格中。 I' not sure what I'm doing wrong. 我不确定自己在做什么错。 I'm pretty sure i'm missing something but I don't know what." 我很确定我会丢失一些东西,但我不知道是什么。”

 import UIKit
 import Photos

 class ViewController: UIViewController  , UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout , UIImagePickerControllerDelegate  , UINavigationControllerDelegate
{

@IBOutlet weak var collectionView: UICollectionView!

@IBAction func addPhoto(_ sender: AnyObject) {

    let picker:UIImagePickerController = UIImagePickerController()
    picker.sourceType = .photoLibrary
    picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!

    picker.delegate = self
    picker.allowsEditing = false
    self.present(picker, animated: true, completion: nil)



}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell: ImageCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ImageCollectionViewCell




    return cell
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
 return   3
}


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {



    if let pickedimage = (info[UIImagePickerControllerOriginalImage] as? UIImage){





    }
    dismiss(animated: true, completion: nil)




}

Collectionview cell Collectionview单元格

import UIKit

 class ImageCollectionViewCell: UICollectionViewCell {


 @IBOutlet weak var imageView: UIImageView!



func configurecell(image: UIImage){


    imageView.image = image





}

} }

For picking Multiple Images from UIImagePicker :- Select Multiple Images 为了从UIImagePicker拾取多个图像:- 选择多个图像

As for populating a CollectionView, Update or store your data in the datasource and call update . 至于填充CollectionView,请在数据源中更新或存储数据,然后调用update

import UIKit

import Photos


class ViewController: UIViewController  , UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout , UIImagePickerControllerDelegate  , UINavigationControllerDelegate

  {


  @IBOutlet weak var myCollectionView: UICollectionView!//Define separate collectionview outlet with a different name than any parameter in any of the function

  var imagesArray = [UIImage]()

  override func viewDidLoad(){
      super.viewDidLoad()
        myCollectionView.delegate = self
        myCollectionView.datasource = self
       }


  @IBAction func addPhoto(_ sender: AnyObject) {

    let picker:UIImagePickerController = UIImagePickerController()
    picker.sourceType = .photoLibrary
    picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!

        picker.delegate = self
        picker.allowsEditing = false
            self.present(picker, animated: true, completion: nil)
         }


  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   let cell: ImageCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ImageCollectionViewCell

      cell.configurecell(imagesArray[indexPath.row])

     return cell

  }


  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

      return imagesArray.count ?? 0

  }



  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedimage = (info[UIImagePickerControllerOriginalImage] as? UIImage){
        imagesArray = [pickedImage,pickedImage,pickedImage]//Will store three selected images in your array
           myCollectionView.reloadData()
   }
   dismiss(animated: true, completion: nil)
  }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM