如何将带有文本的图像编码为属性字符串

问题描述

我使用 NSMutableAttributed 字符串和 NSAttachment 以及以下 Swift imagePickerController 代码将图像插入到 TextKit textView 中:

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 {
            
                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
                    
                    //save to app directory using this function below
                       saveImageToDocumentDirectory(image: pickedImage )
                    
                    //save to UserDefaults directory using this call below as alternate
                       let imageData:NSData = pickedImage.pngData()! as NSData
                     
                       UserDefaults.standard.set(imageData,forKey: "keyp2Image")
                  
                    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)
                        attrString.append(imageattributedText)
                        self.textView.font = UIFont.systemFont(ofSize: 17)
                     
                            
                    }
                   
                   
        })
                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)
                
            }

    }

上面的代码将图像保存到 NSUserDefaults 或应用程序目录,但是,我的问题是我无法使用以下 ViewDidLoad 代码片段重新加载保存的图像:

 let defaults = UserDefaults.standard
    
    if let savednotes = defaults.object(forKey: "newnotes") as? Data {
        let jsonDecoder = JSONDecoder()

        do {
            notes = try jsonDecoder.decode([Note].self,from: savednotes)
        } catch {
            print("Failed to load notes")
        }
    }
  }

问题:使用上述 json 解码器代码同时重新加载图像和文本(文本重新加载正常)的最佳方法是什么。我一直在尝试这个,但没有取得太大的成功。任何建议表示赞赏。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)