gpt4 book ai didi

ios - 在表格 View 中显示从相机拍摄的照片

转载 作者:行者123 更新时间:2023-11-28 13:14:50 24 4
gpt4 key购买 nike

我发现了一些类似的问题,但没有回答我的问题。我希望用户选择一个表格单元格,打开相机,拍照,然后将照片加载到表格单元格的 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
}

}

关于ios - 在表格 View 中显示从相机拍摄的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29440195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com