所以现在我正试图让集合视图单元格使用照片库来更新照片。这是我的代码:
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
答案 0 :(得分:1)
首先,您要在imagePicker
中创建UICollectionViewCell
。
您应该在imagePicker
中创建包含所述ViewController
的{{1}}以及委托方法。然后,在UICollectionView
中,将所选图片设置为适当的didFinishPickingMediaWithInfo
和collectionViewCell
reload