我正在尝试从我的TextView
内的文本内联显示库中的选定图像。我从这里得到这个代码,程序运行没有错误。但是,所选图片不会显示在TextView
中。任何帮助将不胜感激,提前谢谢。
这是我到目前为止所得到的:
// MARK:相机按钮
@IBAction func camera(_ sender: Any) {
let pickerController = UIImagePickerController()
pickerController.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
pickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
pickerController.allowsEditing = true
self.present(pickerController, animated: true, completion: nil)
}
// MARK: show selected inmage in textview
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject]) {
// get image from libary
if let libraryImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
var attributedString :NSMutableAttributedString!
attributedString = NSMutableAttributedString(attributedString:notesTextView.attributedText)
let textAttachment = NSTextAttachment()
textAttachment.image = libraryImage
//textView
let oldWidth = textAttachment.image!.size.width;
let scaleFactor = oldWidth / (notesTextView.frame.size.width - 10);
textAttachment.image = UIImage(cgImage: textAttachment.image!.cgImage!, scale: scaleFactor, orientation: .up)
let attrStringWithImage = NSAttributedString(attachment: textAttachment)
attributedString.append(attrStringWithImage)
notesTextView.attributedText = attributedString;
}
self.dismiss(animated: true, completion: nil)
}