如何将带有图像附件的NSAttributeString保存到rtfd

时间:2018-08-28 01:25:49

标签: ios swift nsattributedstring rtf

我有一个textView并定义了此方法:

func writeWithFile(filePath: String) {
    let documentAttribute = [NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.rtfd]
    do {
        let data = try textView.attributedText.data(from: NSRange(location: 0, length: textView.attributedText.length), documentAttributes: documentAttribute) as NSData
        try data.write(toFile: filePath, options: NSData.WritingOptions.atomicWrite)

        print("File Writed")
    } catch {
        print(error)
    }
}

然后我通过这种方法阅读它:

func readWithFileAndConvertToAttributeString(filePath: String) -> NSAttributedString {
    let newData = NSData(contentsOfFile: filePath)
    let documentAttribute = [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.rtfd]
    do {
        let newAttributeString = try NSAttributedString(data: newData! as Data, options: documentAttribute, documentAttributes: nil)
        return newAttributeString
    } catch {
        print(error)
        return NSAttributedString(string: "ERROR")
    }
}

结果是,没有保存图像附件,我该如何解决?

0 个答案:

没有答案