【问题标题】:Attempting to use photo Library on UICollectionViewCell Class尝试在 UICollectionViewCell 类上使用照片库
【发布时间】:2017-08-08 02:37:48
【问题描述】:

所以现在我正在尝试让集合视图单元格使用照片库来更新照片。这是我的代码:

import UIKit

class viewCellVC: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var bookImage: UIImageView!
@IBAction func addImage(_ sender: Any) {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    imagePicker.allowsEditing = false
    self.window?.rootViewController?.present(imagePicker, animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    bookImage.image = image
    picker.dismiss(animated: true, completion: nil)
}


}

但是,当我尝试运行代码并更新图片时,模拟器无法打开照片库,而是收到以下错误消息(没有结束应用程序,只是无法运行照片库):

2017-08-07 21:35:31.709183-0500 ChuBookElf[2817:66569] Warning: Attempt to present <UIImagePickerController: 0x7f907e0bf800> on <ChuBookElf.ViewController: 0x7f907de072c0> whose view is not in the window hierarchy!

谁能告诉我我做错了什么?我已经做了好几个小时了。

For Reference, here is what the collection view cell looks like

【问题讨论】:

    标签: ios swift uicollectionview uiimagepickercontroller


    【解决方案1】:

    对于初学者,您是在 UICollectionViewCell 中创建 imagePicker

    您应该在包含上述UICollectionView 以及委托方法的ViewController 中创建imagePicker。然后,在didFinishPickingMediaWithInfo 中,将所选图像设置为适当的collectionViewCellreload 你的collectionView

    【讨论】:

    • 我不知道如何将图像从我的 UICollectionViewCell 检索到我的 ViewController。
    • 我建议您查看一些快速编程教程。你会在互联网上找到很多。或者您可以从 AppStore 免费下载 swift 编程指南。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 2023-02-04
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多