How to run imagePicker in tableViewCell

I have a tableView with a cell. In this cell, I have an imageView and a button that closes it. The button has IBAction. When pressed, the button should call up the image. The IBAction code is in the sub-cell.

@IBAction func MyStatusImageButtonAction(sender: AnyObject) {
    // Select Image
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false
    self.presentViewController(imagePicker, animated: true, completion: nil)

I get an error due to this line.

self.presentViewController(imagePicker, animated: true, completion: nil)

The error says: "A value of type" myCell "does not have the name" presentViewController ".

Should I load the imagePicker into a viewController that places the cell and then pass it to the cell? Should I load imagePicker in a cell with a different code? Am I really doing all this wrong?

For clarity, my goal is for the user to load this TableViewController and be able to assign an image only to the image in this cell.

Thanks.

+4
4

, TableViewController .

protocol ImagePickerDelegate {

    func pickImage()
}

var delegate : ImagePickerDelegate?

@IBAction func pickImage(sender: AnyObject) {

    delegate?.pickImage()
}

TableViewController

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("displayImage", forIndexPath: indexPath) as! ImageTableViewCell

    cell.delegate = self

    return cell
}

func pickImage() {

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false
    self.presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    dismissViewControllerAnimated(true, completion: nil)

    var cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as! ImageTableViewCell
    cell.displayImageView.image = image
}

.

+5

a viewController viewController. :

self.view.presentViewController(imagePicker, animated: true, completion: nil)
+1

@truongky Swift 5.

, . . , OP:

protocol ImagePickerDelegate {
    func pickImage() 
}


TableViewController:

func pickImage() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
    imagePicker.allowsEditing = false
    self.present(imagePicker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        let cell:ImageCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! ImageCell
        cell.customImage.image = image
    }

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


, , :

var delegate : ImagePickerDelegate?

@IBOutlet weak var customImage: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    addTapGestureRecognizer(for: customImage)
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

func addTapGestureRecognizer(for view: UIImageView) {
    let tap = UITapGestureRecognizer(target: self, action: #selector(pickImage))
    tap.numberOfTapsRequired = 1
    view.isUserInteractionEnabled = true
    view.addGestureRecognizer(tap)
}

@objc func pickImage() {
    delegate?.pickImage()
}


, .

~

0

Source: https://habr.com/ru/post/1618669/


All Articles