[英]Swift 3 - Custom camera view - display still image of photo taken after button click
[英]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.