从Swift 4中的NSAttributed字符串中获取UIImage和文本

时间:2018-03-29 09:55:00

标签: swift nsattributedstring

我想在iOS中创建一个类似应用的笔记。我已在UITextView上成功添加了文字和图片,并获得了NSTextAttachment的归因字符串。我已使用字段类型" Varchar"将此属性字符串保存在数据库中。但是从数据库中取回数据后,我无法从此属性字符串中获取文本和图像。

这是我的示例代码:

var imagesArray = [Any]()
        textView.attributedText = attributedString
        textView.attributedText.enumerateAttribute(NSAttributedStringKey.attachment, in: NSRange(location: 0, length: textView.attributedText.length), options: [], using: {(value,range,stop) -> Void in
            if (value is NSTextAttachment) {
                let attachment: NSTextAttachment? = (value as? NSTextAttachment)
                var image: UIImage? = nil

                if ((attachment?.image) != nil) {
                    image = attachment?.image
                } else {
                    image = attachment?.image(forBounds: (attachment?.bounds)!, textContainer: nil, characterIndex: range.location)
                }

                if image != nil {
                    imagesArray.append(image!)
                }
            }
        })

这里是" value"总是返回零。

有谁能告诉我这段代码有什么问题?

0 个答案:

没有答案
相关问题