在iOS的UITextView中附加后无法显示图像

3z6pesqy  于 2023-02-26  发布在  iOS
关注(0)|答案(2)|浏览(208)

我想插入图像到uitextview像在iphone的笔记应用程序。我成功地插入图像,但当我保存笔记,我不能显示我插入的图像时,点击查看笔记的详细信息。我该如何解决这个问题?The note before savingThe note after saving
下面是我的代码:Add image after choosing from libraryInitial set up when navigate to detail view

ht4b089n

ht4b089n1#

在**func setup()**中,您需要生成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

BUT:如果你在note.content中存储了一个完整的attributedString,那么就传递self.tvContent.attributedString

sulc1iza

sulc1iza2#

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
                                }
                            }
                        }
                    }
                }

相关问题