尝试在 UICollectionViewCell 上使用照片库 Class
Attempting to use photo Library on UICollectionViewCell Class
所以现在我正在尝试让集合视图单元格使用照片库来更新照片。这是我的代码:
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
首先,您正在 UICollectionViewCell
中创建 imagePicker
。
您应该在包含上述 UICollectionView
的 ViewController
中创建 imagePicker
以及委托方法。然后,在 didFinishPickingMediaWithInfo
中,将所选图像设置为适当的 collectionViewCell
和 reload
您的 collectionView
所以现在我正在尝试让集合视图单元格使用照片库来更新照片。这是我的代码:
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
首先,您正在 UICollectionViewCell
中创建 imagePicker
。
您应该在包含上述 UICollectionView
的 ViewController
中创建 imagePicker
以及委托方法。然后,在 didFinishPickingMediaWithInfo
中,将所选图像设置为适当的 collectionViewCell
和 reload
您的 collectionView