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

问题描述 投票:0回答:2

我想在 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
2个回答
0
投票

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

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


0
投票
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
                                }
                            }
                        }
                    }
                }
© www.soinside.com 2019 - 2024. All rights reserved.