2017-08-08 22 views
0

所以現在我試圖讓集合視圖單元格使用照片庫來更新照片。這裏是我的代碼:試圖在UICollectionViewCell類上使用照片庫

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

回答

1

對於初學者來說,你是在一個UICollectionViewCell創建imagePicker

您應該在包含UICollectionView以及委託方法的ViewController中創建imagePicker。然後,在didFinishPickingMediaWithInfo,所選擇的圖像設置爲適當collectionViewCellreloadcollectionView

+0

我不知道如何從我的UICollectionViewCell形象,我的ViewController雖然。 –

+0

我會建議看一些快速編程的教程。你會在互聯網上找到一噸。或者您可以免費從AppStore下載快速編程指南。 – Malik