简体   繁体   中英

Display a photo taken from camera in a table view

I've found a few questions similar but nothing answered my question. I want the user to select a table cell, have the camera open, take a photo, and have that photo load into the imageView in the table cell. Here is a snippet of my code so far. I'm just lost as how to add this photo into the table cell. Thanks!

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    var photo = info[UIImagePickerControllerOriginalImage] as? UIImage
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return bedroomCells.count
}

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

    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell

    let row = indexPath.row
    cell.textLabel?.text = bedroomCells[row]
    //cell.imageView?.image =
    return cell
}

Try this out, seems to be working for me:

class ExampleTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    struct TestStruct {
        var text: String?
        var image: UIImage?
    }

    var imagePicker = UIImagePickerController()
    var bedroomCells = [TestStruct]()
    var lastSelectedIndex: NSIndexPath?
    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
        for var i = 0; i < 10 ; i++ {
            var entry = TestStruct(text: "\(i)", image: nil)
            bedroomCells.append(entry)
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return bedroomCells.count;
    }


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

        let cell = tableView.dequeueReusableCellWithIdentifier("sampleCell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel?.text = bedroomCells[indexPath.row].text
        cell.imageView?.image = bedroomCells[indexPath.row].image

        return cell
    }


    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        self.lastSelectedIndex = indexPath // Save the selected index
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }


    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        imagePicker.dismissViewControllerAnimated(true, completion: nil)
        var photo = info[UIImagePickerControllerOriginalImage] as? UIImage
        bedroomCells[lastSelectedIndex!.row].image = photo // Set the image for the selected index
        tableView.reloadData() // Reload table view
    }

}

Try this sample git code, I have addressed common tableviewCell problems using Custom cell, PrepareforReuse and willDisplay cell to show image are reset when not used. Also this contains reusable cameraAlert which can be called from any view controller, with pick from gallery or camera capture.

https://github.com/shruezee/MultipleImage-TableViewCell-CameraCapture

Further to muneeb answer, removing images in prepareForReuse() helps resolving, scrolling issue. In case you have single image just make it nil. otherwise remove all from superview

override func prepareForReuse() {
        super.prepareForReuse()

//        cellImageView.image = nil
        removeAllImagesFromStack()
    }

func removeAllImagesFromStack() {
    for each in imageStackview.subviews {
        if let imageviewFromStack = each as? UIImageView {
            imageviewFromStack.removeFromSuperview()
        }
    }
}

I also had add image button inside tableViewCell so added callback method to get action inside tabView , detail implementation inside git link

var addImagePressed: ((UIStackView) -> Void)?

    @IBAction func addImageButtonPressed(_ sender: UIButton) {
        addImagePressed?(imageStackview)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM