【问题标题】:Can not show image after attach in UITextView in iOS在 iOS 中的 UITextView 中附加后无法显示图像
【发布时间】:2023-02-21 17:46:15
【问题描述】:

我想将图像插入到 uitextview 中,就像 iphone 中的笔记应用程序一样。我成功插入了图像,但是当我保存笔记时,我无法显示我在单击以查看笔记详细信息时插入的图像。我该如何解决? The note before saving The note after saving

这是我的代码: Add image after choosing from library Initial set up when navigate to detail view

【问题讨论】:

  • 请复制/粘贴代码,不仅是截图

标签: ios swift mobile uikit uitextview


【解决方案1】:

功能设置()您需要生成 attributeString 并将图像保存在附件中。

let attachment = NSTextAttachment()
attachment.bounds = CGRect(x: 0, y: 0, width: newImageWidth, height: newImageHeight)
attachment.image = attachImage // saving from cache or storage memory
var attributedString = NSMutableAttributedString(attributedString: "your note text")
attributedString.append(NSAttributedString(attachment: attachment))
self.tvContent.attributedText = attributedString

但是:如果您将整个 attributedString 存储在 note.content 中。然后传递它 self.tvContent.attributedString

【讨论】:

    【解决方案2】:
    private func setup() {
            tfTitle.layer.borderWidth = 1
            tfTitle.layer.borderColor = UIColor.darkGray.cgColor
            tfTitle.text = note.title
            tvContent.layer.borderWidth = 1
            tvContent.layer.borderColor = UIColor.darkGray.cgColor
            tvContent.text = note.content
    //        var attributedString: NSMutableAttributedString!
    //        attributedString = NSMutableAttributedString(attributedString: self.tvContent.attributedText)
    //        tvContent.attributedText = attributedString
    //        tvContent.attributedText = NSAttributedString(string: note.content)
    //        tvContent.textStorage.insert(NSAttributedString(string: note.content), at: 0)
        }
      
        //Insert the image    
            func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
                        picker.dismiss(animated: true, completion: nil)
                        results.forEach { result in
                            result.itemProvider.loadObject(ofClass: UIImage.self) { object, error in
                                if let image = object as? UIImage, error == nil {
                                    self.images.append(image)
                                    DispatchQueue.main.async {
                                        let attachImage = self.images[self.images.count - 1]                        
                                        //Caculate new size
                                        let newImageWidth = (self.tvContent.bounds.size.width)
                                        let scale = newImageWidth/image.size.width
                                        let newImageHight = image.size.height*scale
                                        
                                        let attachment = NSTextAttachment()
                                        attachment.bounds = CGRect.init(x: 0, y: 0, width: newImageWidth, height: newImageHight)
                                        attachment.image = attachImage
                                        
                                        var attributedString: NSMutableAttributedString!
                                        attributedString = NSMutableAttributedString(attributedString: self.tvContent.attributedText)
                                        
                                        let attachString = NSAttributedString(attachment: attachment)
                //                        self.tvContent.textStorage.insert(
                //                            attachString,
                //                            at: self.tvContent.selectedRange.location)
                                        attributedString.append(attachString)
                                        self.tvContent.attributedText = attributedString
                                    }
                                }
                            }
                        }
                    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多