我在桌面视图中有一个注册表单,在该注册表单中,用户可以选择个人资料图片。
如何在选中图像的情况下更新tableview单元格中的图像?
我如何允许细胞。在" func imagePickerController()"哪个不在tableview内?
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 :(得分:1)
在didfinishPickingMediaWithInfo内部重用你需要添加图像的单元格,并简单地将拾取的图像分配给单元格内的imageView。
简单!!
答案 1 :(得分:0)
var orginalImage
if orginalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
tableView.reloadData()
}
//现在您可以在cellForRowAtIndexPath
中使用此图像cell.profileImage.image = orginalImage
答案 2 :(得分: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)
}
答案 3 :(得分:0)
你必须
- 将本地变量添加到控制器。
- 从图像选择器设置为var图像
- 重新加载单元格/表格
醇>
例如
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
}
}
答案 4 :(得分:-1)
在AppDelegate中制作图像变量,如
var globalImage = UIImage()
并且您获得价值的地方将其发送到AppDelegate。制作AppDelgate对象如下
let appDelegate = UIApplication.shared.delegate as! AppDelegate
将值传递给app委托变量
self.appDelegate.globalImage = "your image here"
并获取您想要获取的值,如下所示
在你的情况下,它在TableViewCell中,所以在cellforRowAt下
cell.profileImage.image = appDelegate.globalImage
传递的值