I am adding both text and image in UITextView to part of my app ( similar to the notes app ). The user can write a text and/or then select a picture from the photo library or take a picture ( with image picker).
I managed to add the text and the image into the UITextView. I can save the text but can’t figure out how to save the image. I have looked very similar questions but still having a hard time getting the code. I have tried to do it with userDefaults but apparently it’s not the right way. Maybe documents directory or file path but I don’t know how to do that. Any help will be much appreciate it.
This is the code I have:
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)
}
}