I am creating an NSMutableAttributedString
, attaching an image, getting string value from html representation and saving it, found from SO answer. It can get the relative file path on string. But If I save it as doc file it does not show image. Probably doc software can not find that location and doc's should not dynamically load data from file location. Is there any way to embed image while saving as doc file?
Heres what I'm doing:
let fullString = NSMutableAttributedString()
// create our NSTextAttachment
let image1Attachment = NSTextAttachment()
image1Attachment.image = UIImage(named: "red-roses.jpg") //NSIMage
// wrap the attachment in its own attributed string so we can append it
let image1String = NSAttributedString(attachment: image1Attachment)
// add the NSTextAttachment wrapper to our full string, then add some more text.
fullString.append(image1String)
fullString.append(NSAttributedString(string: "End of text"))
// draw the result in a label
label.attributedText = fullString
print(fullString.mutableString)
//imageView.image = UIImage(named: "red-roses")
do {
let htmlData = try fullString.data(from: .init(location: 0, length: fullString.length),
documentAttributes: [.documentType: NSAttributedString.DocumentType.html])
let htmlString = String(data: htmlData, encoding: .utf8) ?? ""
print(htmlString)
//try htmlString.writeTo(fileLocation, "filename.doc")
} catch {
print(error)
}
It can show relative file path on htmlString
. The saved doc will show only strings and no image. Is there any way to embed image on doc?