繁体   English   中英

在表格视图中显示从相机拍摄的照片

[英]Display a photo taken from camera in a table view

我发现了一些类似的问题,但没有回答我的问题。 我希望用户选择一个表格单元格,打开相机,拍照,然后将该照片加载到表格单元格的 imageView 中。 到目前为止,这是我的代码片段。 我只是失去了如何添加照片到表格单元格。 谢谢!

 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
}

试试这个,似乎对我有用:

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
    }

}

试试这个示例 git 代码,我已经使用自定义单元格解决了常见的 tableviewCell 问题,PrepareforReuse 和 willDisplay 单元格显示图像在不使用时被重置。 这还包含可重用的 cameraAlert,可以从任何视图控制器调用,从图库或相机捕获中选择。

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

除了muneeb 的回答之外,在 prepareForReuse() 中删除图像有助于解决滚动问题。 如果您有单个图像,只需将其设为 nil。 否则从超级视图中删除所有内容

override func prepareForReuse() {
        super.prepareForReuse()

//        cellImageView.image = nil
        removeAllImagesFromStack()
    }

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

我还在tableViewCell添加了图像按钮,因此添加了回调方法来获取tabView动作,git 链接中的详细实现

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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