简体   繁体   English

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

[英]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.我希望用户选择一个表格单元格,打开相机,拍照,然后将该照片加载到表格单元格的 imageView 中。 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.试试这个示例 git 代码,我已经使用自定义单元格解决了常见的 tableviewCell 问题,PrepareforReuse 和 willDisplay 单元格显示图像在不使用时被重置。 Also this contains reusable cameraAlert which can be called from any view controller, with pick from gallery or camera capture.这还包含可重用的 cameraAlert,可以从任何视图控制器调用,从图库或相机捕获中选择。

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

Further to muneeb answer, removing images in prepareForReuse() helps resolving, scrolling issue.除了muneeb 的回答之外,在 prepareForReuse() 中删除图像有助于解决滚动问题。 In case you have single image just make it nil.如果您有单个图像,只需将其设为 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我还在tableViewCell添加了图像按钮,因此添加了回调方法来获取tabView动作,git 链接中的详细实现

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

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

暂无
暂无

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

相关问题 Swift 3 - 自定义相机视图 - 显示按钮点击后拍摄的照片的静止图像 - Swift 3 - Custom camera view - display still image of photo taken after button click 异步加载从UIImagePickerController相机拍摄的照片 - Loading a photo taken from UIImagePickerController camera asynchronously 如何检测照片是否来自前置摄像头 - How to detect if photo taken from front camera 显示从图库到表视图中选择的照片 - Display Photo Selected From Gallery into Table View 如何在从图片库拍摄的uiimageview中显示拾取的图像到另一个视图控制器中的另一个uiimageview中? - How to display picked image in a uiimageview taken from the photo library to another uiimageview in a different view controller? 是否可以检查图像是由相机拍摄的还是由JavaScript从照片库上传的? - Is it possible to check if an image is taken by camera or uploaded from photo library by JavaScript? 关闭应用程序后,从相机拍摄的照片不会出现在图像视图中 - A photo taken from the camera does not appear on the imageview after closing the application 如何检测照片库中的图像是由iPhone相机拍摄还是导入 - how to detect if image from photo library was taken by iPhone camera or imported 只有在相机拍摄时才将图像保存到照片库,如果从照片库中选择则不能 - Save images to photo library ONLY if they are taken by camera and not if chosen from photo library 如何在CollectionView中显示照片库或相机中的选定图像? 图库视图 - How can I display selected images from the Photo Library or Camera in a CollectionView? Gallery view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM