首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionViewCell不显示从UIImagePickerController拾取的图像

UICollectionViewCell不显示从UIImagePickerController拾取的图像
EN

Stack Overflow用户
提问于 2019-04-11 06:44:20
回答 2查看 447关注 0票数 2

有没有人能看到这里有什么错误阻止了单元格"photoInTheCellnow“在图像选择器中显示UIImage值

代码语言:javascript
复制
import UIKit
import Photos
import PhotosUI

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

    @IBOutlet weak var collectionView: UICollectionView!

    var photosInTheCellNow = [UIImage]()
    var imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.delegate = self
        collectionView.dataSource = self
        imagePicker.delegate = self
    }

    @IBAction func openLibrary(_ sender: UIBarButtonItem) {

        let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()

        switch photoAuthorizationStatus {
        case .authorized: print("Access is granted by user")
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization({ (newStatus) in print("status is \(newStatus)"); if newStatus == PHAuthorizationStatus.authorized { print("success") } })
        case .restricted: print("User do not have access to photo album.")
        case .denied: print("User has denied the permission.")  
        }

        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self


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

            let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

            photosInTheCellNow.append(image)
            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 photosInTheCellNow.count
        }


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

            return cell
        }

        func register(_ cellClass: PhotosCollectionViewCell, forCellWithReuseIdentifier identifier: String) { }
   }
}

这是自定义单元格类

代码语言:javascript
复制
import UIKit

class PhotosCollectionViewCell: UICollectionViewCell {


    @IBOutlet weak var imageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func prepareForReuse() {
        super.prepareForReuse()
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-11 07:42:46

正如@RaziTiwana所提到的,您需要将图像分配给cellForItemAt:indexPath中的集合视图单元格。

代码语言:javascript
复制
cell.imageView.image =  photosInTheCellNow[indexPath.row]

一旦选择了图像,您还需要更新collectionView。您将图像添加到了photosInTheCellNow属性,但是还没有让collectionView知道数据源发生了更改。您可以通过调用collectionView.reloadData()重新加载整个collectionView,也可以只插入最后一行,如下所示。

代码语言:javascript
复制
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
       let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

       photosInTheCellNow.append(image)
       let indexPath = IndexPath(item: photosInTheCellNow.count-1, section: 0)
       collectionView.insertItems(at: [indexPath])
       picker.dismiss(animated: true, completion: nil)
    }
票数 1
EN

Stack Overflow用户

发布于 2019-04-11 07:33:02

你不见了

代码语言:javascript
复制
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotosCollectionViewCell", for: indexPath) as! PhotosCollectionViewCell

    cell.imageView.image =  photosInTheCellNow[indexPath.row]

    return cell
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55622286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档