我將 UITextView 中的文本和影像添加到我的應用程式的一部分(類似于注釋應用程式)。用戶可以撰寫文本和/或然后從照片庫中選擇圖片或拍照(使用影像選擇器)。
我設法將文本和影像添加到 UITextView 中。我可以保存文本,但不知道如何保存影像。我看過非常相似的問題,但仍然很難獲得代碼。我試圖用 userDefaults 來做,但顯然這不是正確的方法。也許是檔案目錄或檔案路徑,但我不知道該怎么做。任何幫助將不勝感激。
這是我的代碼:
class NotesViewController: UIViewController, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var textView: UITextView!
var pickImage: UIImage!
var attString = NSAttributedString()
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
self.isModalInPresentation = true
textView.keyboardDismissMode = .interactive // .onDrag
textView.becomeFirstResponder()
textView.textColor = UIColor.self.init(red: 222/255, green: 215/255, blue: 191/255, alpha: 1.0)
textView.font = UIFont(name: "Trebuchet MS", size: 19)
textView.backgroundColor = UIColor.clear
textView.tintColor = UIColor.self.init(red: 141/255, green: 124/255, blue: 85/255, alpha: 1.0)
textView.layer.masksToBounds = false
if let value = userDefault.value(forKey: myNotes) as? String {
textView.text = value
}
textView.alwaysBounceVertical = true
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
pickImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
//create and NSTextAttachment and add your image to it.
let attachment = NSTextAttachment()
attachment.image = pickImage
//calculate new size. (-20 because I want to have a litle space on the right of picture)
let newImageWidth = (textView.bounds.size.width - 15 )
let scale = newImageWidth/pickImage.size.width
let newImageHeight = pickImage.size.height * scale - 20
//resize this
attachment.bounds = CGRect.init(x: 0, y: 0, width: newImageWidth, height: newImageHeight)
//put your NSTextAttachment into and attributedString
attString = NSAttributedString(attachment: attachment)
//add this attributed string to the current position.
textView.textStorage.insert(attString, at: textView.selectedRange.location)
textView.selectedRange.location = 1
textView.textColor = UIColor.self.init(red: 222/255, green: 215/255, blue: 191/255, alpha: 1.0)
textView.font = UIFont(name: "Trebuchet MS", size: 19)
textView.backgroundColor = UIColor.clear
textView.tintColor = UIColor.self.init(red: 141/255, green: 124/255, blue: 85/255, alpha: 1.0)
textView.keyboardDismissMode = .interactive
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
@objc func saveText() {
userDefault.setValue(textView.text, forKey: myNotes)
dismiss(animated: true, completion: nil)
}
}
uj5u.com熱心網友回復:
有一個重要的事情你需要知道:
- 您自定義的所有內容,例如添加影像、添加圖示……您始終
NSAttributedString不是純文本。
所以在你的 funcsaveText你打電話:
userDefault.setValue(textView.text, forKey: myNotes)
意味著您僅存盤來自textViewwhich is的純文本,textView.text但不存盤所有特殊屬性組合在您的textViewwhich is 中textView.attributedText。這是您在保存時沒有隨身攜帶影像的主要原因。
為此,您需要創建兩個功能:一個用于存盤屬性文本UserDefault,另一個UserDefault用于顯示
對于第一個,它將您的屬性文本存盤到UserDefault. 我們不能直接保存NSAttributedString,UserDefault所以我們需要Data先將其轉換為并存盤它。
func saveAttributedTextToUserDefault(attributedText: NSAttributedString, key: String) {
do {
let data = try attributedText.data(from: NSRange(location: 0, length: attributedText.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtfd])
UserDefaults.standard.setValue(data, forKeyPath: key)
} catch {
print(error)
}
}
第二個,只需從中獲取資料UserDefault并將其轉換回NSAttributedString
func getAttributedTextFromUserDefault(key: String) -> NSAttributedString {
if let dataValue = UserDefaults.standard.value(forKey: key) as? Data {
do {
let attributeText = try NSAttributedString(data: dataValue, documentAttributes: nil)
return attributeText
} catch {
print("error: ", error)
}
}
return NSAttributedString()
}
用途
override func viewDidLoad() {
// your others code
let attributedText = self.getAttributedTextFromUserDefault(key: "test")
textView.attributedText = attributedText
}
@objc func saveText() {
// take attributedText and save it
self.saveAttributedTextToUserDefault(attributedText: textView.attributedText, key: "test")
dismiss(animated: true, completion: nil)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/515079.html
