Ios 表视图单元格内图像的Swift 3“didFinishPickingMediaWithInfo”
我在tableview中有一个注册表单,用户可以在该注册表单中选择一个配置文件图像 如何使用选定的图像更新tableview单元格中的图像 我该如何允许手机。在func imagePickerController中,哪个不在tableview中 //现在您可以在cellForRowAtIndexPath中使用此图像Ios 表视图单元格内图像的Swift 3“didFinishPickingMediaWithInfo”,ios,swift,uitableview,swift3,Ios,Swift,Uitableview,Swift3,我在tableview中有一个注册表单,用户可以在该注册表单中选择一个配置文件图像 如何使用选定的图像更新tableview单元格中的图像 我该如何允许手机。在func imagePickerController中,哪个不在tableview中 //现在您可以在cellForRowAtIndexPath中使用此图像 cell.profileImage.image = orginalImage 在AppDelegate中设置图像变量,如 var globalImage = UIImage()
cell.profileImage.image = orginalImage
在AppDelegate中设置图像变量,如
var globalImage = UIImage()
在哪里获得值,请将其发送给AppDelegate。使AppDelgate对象如下所示
let appDelegate = UIApplication.shared.delegate as! AppDelegate
将值传递给应用程序委托变量
self.appDelegate.globalImage = "your image here"
并在任何您想要获取的地方获取您的值,如下图所示
let appDelegate = UIApplication.shared.delegate as! AppDelegate
在您的情况下,它位于TableViewCell中,因此位于cellforRowAt下
cell.profileImage.image = appDelegate.globalImage
传递的值只需在didFinishPickingMediaWithInfo方法中创建单元格文件对象
Inside didfinishPickingMediaWithInfo重用您需要添加图像的单元格,只需将拾取的图像指定给单元格内的imageView即可 简单 你必须 将局部变量添加到控制器。 从图像选择器设置为var图像 重新加载单元格/表格 比如说
“什么是单元格”的错误似乎更多。将原始图像设为全局图像,并在选项卡视图单元格中传递此图像。您不能在其中创建单元格您需要使用Tableview代理您不能像现在这样访问单元格使用应用程序代理来存储图像,该图像将仅在一个viewController中使用,这是不必要的,而且是一种过度使用。OP最好在所述viewController内创建一个全局变量,如@Latif kumar的评论中所述。工作完美。我做了,但我的声誉低于15,所以它说它不会公开显示。有一个类似的问题,通过一些工作,这个答案完全解决了我的问题。谢谢@Jaydip
cell.profileImage.image = appDelegate.globalImage
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)
}
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
}
}