[英]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.