uiimagepickercontroller - 如何在 TableView DetailViewConroller 的正确 IndexPath.row 中插入图像?
问题描述
我希望将照片中的图像插入到当前选定的 indexpath 行的 TableView DetailViewController 中。所选图像作为 NSAttachment 被正确插入到属性字符串中,除了它被全局插入到所有注释索引路径中,而我希望仅将图像插入到单个索引路径,即当前选定的索引路径行(即第 3 行)。我的问题是我无法弄清楚如何将 indexpath 调用与 imagePickerController 属性字符串代码集成,以便图像仅出现在一个 Tableview DetailView 行(即第 3 行)中。任何建议都非常感谢。基本代码如下:
数据模型:这些字符串将是表格视图单元格的数据
var note: [String] = ["This is note #0.", "This is note #1.", "This is note #2.", "This is note #3.", "This is note #4.","This is note #5.","This is note #6.","This is note #7.","This is note #8.","This is note #9."]
为每个表格视图行创建一个单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = (self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell?)!
let thenote = self.note[indexPath.row]
let font = UIFont.preferredFont(forTextStyle: .headline)
let textColor = UIColor(red: 0.175, green: 0.458, blue: 0.831, alpha: 1)
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: textColor, .font: font,]
let attributedString = NSMutableAttributedString(string: thenote, attributes: attributes)
cell.textLabel?.attributedText = attributedString
return cell
}
函数将从 Photos 中选择的图像插入名为“imageattributedText”的属性字符串中
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]
picker.dismiss(animated: true, completion: nil)
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
if let imgUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL{
let imgName = imgUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let localPath = documentDirectory?.appending(imgName)
let photoURL = URL.init(fileURLWithPath: localPath!)
let alert = UIAlertController(title: "Insert Image", message: "Do you wish to insert the selected image?", preferredStyle: UIAlertController.Style.alert)
let defaultAction = UIAlertAction(title: "Insert Image", style: UIAlertAction.Style.default, handler: { [self]
(action:UIAlertAction!) -> Void in
let attachment = NSTextAttachment()
attachment.image = pickedImage
let imageattributedText = NSMutableAttributedString(attachment: attachment)
if let selectedRange = textView.selectedTextRange {
let cursorPosition = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
textView.textStorage.insert(imageattributedText, at:cursorPosition)
let attrs = [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .body), .foregroundColor: UIColor.label]
let attrtext = NSMutableAttributedString(string: "\n", attributes: attrs)
attrtext.append(imageattributedText)
self.textView.font = UIFont.systemFont(ofSize: 17)
do {
// note that fileURL is a global variable so that it can read back the saved rtfd document data created below
fileURL = photoURL
// convert URL! to String
let path:String = fileURL.path
// convert global variable attrString to data
let data = try attrtext.data(from: NSRange(location: 0, length: attrtext.length),
documentAttributes: [.documentType:
NSAttributedString.DocumentType.rtfd])
// write data to the local app directory
try data.write(to: URL(fileURLWithPath: path))
// print the path for debugging reference to make sure it is saved in Xcode Simulator
print("File was saved to:\(path)")
} catch {
print("\(error.localizedDescription)")
}
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: {
(action:UIAlertAction!) -> Void in
print("Cancel")
})
alert.addAction(cancelAction)
alert.addAction(defaultAction)
present(alert, animated: true, completion: nil)
}
}
}**
解决方案
推荐阅读
- html - 更改 index.html 的内容是否需要重新启动 IIS 站点?
- android - Firebase 查询 2 种相同的方法,一种有效,另一种无效
- ios - CircleCI for iOS - 缓存 cocoapods 依赖项
- vba - 如何修复“运行时错误‘3464’:条件表达式中的数据类型不匹配。” 在 MS 访问
- python - Dynamodb 读取性能差
- haskell - 是否可以将 asciidoc 与 hakyll 一起使用?
- django - Django + Gunicorn + SCRIPT_NAME
- c# - cv::VideoCapture 在 C# 项目中从 DLL 使用时不会打开文件或流
- swift - 在 Swift 中的同一个 VC 上点击按钮后,有没有办法显示以前隐藏在早期代码行中的 UI 元素?
- sql-server - 禁用目标数据库的自动填充以进行数据库还原