2017-08-08 84 views
-1

我在桌面視圖中註冊了一個表單,用戶可以在該表單中選擇一個配置文件圖像。Swift 3「didFinishPickingMediaWithInfo」用於桌面單元格內的圖像

如何在選中圖像的情況下更新tableview單元格中的圖像?

如何讓細胞。裏面的「func imagePickerController()」不在tableview內?

Click for code

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

if let orginalImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 

cell.profileImage.image = orginalImage 

} 

else { print ("error") } 

picker.dismiss(animated: true, completion: nil) 

} 
+0

錯誤似乎更多的是什麼細胞。 –

+0

將原始圖像創建爲全局圖像並將其傳遞到tabview單元格中。你不能在那裏創建單元格 –

+0

你需要使用TableView的代表你不能訪問單元格,因爲你現在正在做的 –

回答

0

只需在didFinishPickingMediaWithInfo方法中創建您的單元格文件對象。

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


     //cell file object 
     let cell = tblView.cellForRow(at: IndexPath.init(row: 0, section: 0)) as! ProfileTableViewCell //cell file object 

     let imgTemp = info[UIImagePickerControllerOriginalImage] as? UIImage// get image from imagePickerViewController 
     cell.imgViewProfile.image = imgTemp 
     cell.imgViewProfile.layer.cornerRadius = cell.imgViewProfile.frame.size.width/2 // make image circle 
     cell.imgViewProfile.layer.masksToBounds = true 
     appDelegate.window?.rootViewController?.dismiss(animated: true, completion: nil) 
    } 
+0

中提到,那麼在上述viewController中創建一個全局變量會更好。完美的作品。 –

+0

然後請投票和正確的我的回答 – Jaydip

+0

@BrettTaylor請upvote我的回答 – Jaydip

0
var orginalImage 

if orginalImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 

tableView.reloadData() 

} 

//現在你可以在的cellForRowAtIndexPath

cell.profileImage.image = orginalImage 
-1

製作圖像變量使用此圖片在你的AppDelegate像

var globalImage = UIImage() 

,並在那裏你正在獲取價值發送給AppDelegate。使AppDelgate對象像下面

let appDelegate = UIApplication.shared.delegate as! AppDelegate 

價值傳遞給應用程序的委託變量

self.appDelegate.globalImage = "your image here" 

,讓你的價值觀,無論你想獲取像下面

你的情況

其TableViewCell所以在cellforRowAt

cell.profileImage.image = appDelegate.globalImage 

通過的值

+0

使用應用程序委託來存儲一個只能在一個viewController中使用的圖像是不必要的和過度殺傷。如果在@Latif kumar的評論 – Malik

1

內didfinishPickingMediaWithInfo重用其中U需要添加圖像和簡單的圖像拾取分配給小區內的ImageView的細胞。

簡單!

0

你必須只

  1. 添加局部變量控制器。
  2. 設置爲VAR圖像從圖像拾取
  3. 刷新細胞/表

例如

class ImageViewController: UIImagePickerControllerDelegate, UITableViewDelegate, UITableViewDataSource { 

     var image: UIImage? 
     weak var table: UITableView! 

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

      if let orginalImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
      self.image = orginalImage 
     } else { print ("error") } 
      picker.dismiss(animated: true, completion: nil) 
     } 

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

      let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) 

      return cell 
     } 

     override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
      let _cell = cell 

      _cell.imageView.image = self.image 
     } 

}