2017-08-08 86 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 
     } 

}