gpt4 book ai didi

ios - 尝试学习如何将图像从照片库加载到 Collection View

转载 作者:行者123 更新时间:2023-11-30 13:05:31 25 4
gpt4 key购买 nike

我正在尝试学习如何使用 Collection View ,我想要做的是能够访问照片库中的照片并将它们放入 Collection View 中。

我遇到的问题是当我单击照片时,让照片显示在单元格中,但单元格中没有任何内容。我不确定我做错了什么。我很确定我错过了一些东西,但我不知道是什么。”

 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)




}

Collection View 单元格

import UIKit

class ImageCollectionViewCell: UICollectionViewCell {


@IBOutlet weak var imageView: UIImageView!



func configurecell(image: UIImage){


imageView.image = image





}

}

最佳答案

用于从 UIImagePicker 中选取多个图像:- Select Multiple Images

至于填充 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)
}

关于ios - 尝试学习如何将图像从照片库加载到 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39424057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com