Ziel ist es, dem Benutzer zu ermöglichen, NSImage
(s) zu einem NSAttributedString
in einem NSTextView
hinzuzufügen, und dann den Prozess umzukehren und das Bild (die Bilder) zu extrahieren. Mit Code von here, können Sie Bilder hinzufügen und inline anzeigen lassen.Wie NSMage aus NSTextAttachment in NSTextView ziehen?
let attributedText = NSMutableAttributedString(string: string, attributes: attributes as? [String : AnyObject])
let attachment = NSTextAttachment()
let imageTest = NSImage(named:"sampleImage") as NSImage?
let attachmentCell: NSTextAttachmentCell = NSTextAttachmentCell.init(imageCell: imageTest)
attachment.attachmentCell = attachmentCell
let imageString = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
attributedText.append(imageString)
textView.textStorage?.setAttributedString(attributedText)
Kann man das so weit als legit (Nicht-Null) NSTextAttachment
, aber unsicher dekonstruieren, wie das Bild zu extrahieren.
if (textView.textStorage?.containsAttachments ?? false) {
let runs = textView.textStorage?.attributeRuns
if runs != nil {
for run in runs! {
var r = NSRange()
let att = run.attributes(at: 0, effectiveRange: &r)
let z = att[NSAttachmentAttributeName] as? NSTextAttachment
if z != nil {
Swift.print(z!)
// z!.image and z!.contents are both nil
}
}
}
}
Ich schätze jede Anleitung, wie man das Bild (s) daraus zieht. Vielen Dank.
Ich habe eine ähnliche Lösung in Objective-C und iOS ersetzt: http://stackoverflow.com/questions/29152660/extract-uiimage-from-nsattributed-string Aber Ich denke, es kann angewendet werden (vielleicht mit ein paar Änderungen für OS X). – Larme
Was in iOS für 'UIImage' funktioniert funktioniert nicht für MacOS und' NSImage', so scheint es. 'image' ist null und' imageForBounds' gibt etwas zurück, was wie ein leeres Dokument aussieht. – JKaz
Hat 'z.content' einen Wert? Oder gibt es irgendwelche Eigenschaften, die nicht null sind? – Larme