我有一个 UITableViewCell 按钮来拍摄图像并将其放回单元格中。当我调用 UIImagePickerController 并选择/获取图像时,它应该调用以下函数
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject], sender:AnyObject)
由于某种原因,它没有调用此函数,因此,我无法将所选 UIImage 放置到当前单元格中。任何帮助将不胜感激。
这是按钮代码
@IBAction func takePhoto(sender: AnyObject) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
let actionSheet = UIAlertController(title: "Choose image souruce", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Take Image", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)
}
这是 didFinishPickingMediaWithInfo 上的 imagePickerController 函数
self.reloadData 是运行 UITableView 并重新加载数据的函数。
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject], sender:AnyObject) {
var btnPos: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
//Gets the chosen cell indexPath
var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(btnPos)!
let currentCell:TableViewCell = tableView.cellForRowAtIndexPath(indexPath) as TableViewCell
let iamgePicked:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
currentCell.cellImageOulet.image = iamgePicked
picker.dismissViewControllerAnimated(true, completion: nil)
self.loadData()
}