繁体   English   中英

图像插入后NSTextView不会更新

[英]NSTextView not updating after Image insert

我的应用程序(swift,OSX,Xcode,所有内容的最新版本)都有一个NSTextView,我允许用户输入。 NSTextView启用了RichText,图形和NSInspectorBar。

我将NSTextView的内容加载/保存为webarchive格式(发牢骚 - 但我需要一种包含图像的可移植格式)。

到现在为止还挺好。 一切都有效...除了一个小细节。 当我将图像插入TextView时,它不会显示。 但它就在那里,因为如果我保存并加载......它就会出现。

以下是我将图像插入NSTextView的方法(我从NSOpenPanel获取图像的路径:

    guard let theImage = NSImage(contentsOfURL: openPanel.URL!) else   {
        showErrorMessage("Unable to load image")
        return
    }
    // self.textEditor is the NSTextView
    guard let store = self.textEditor.textStorage else { abort() }

    let attachment = NSTextAttachment()
    attachment.image = theImage
    let attrString = NSAttributedString(attachment: attachment)       

    let range = self.textEditor.selectedRange()
    store.replaceCharactersInRange(range, withAttributedString: attrString)

我认为它相当直接:加载图像,添加到attributionString,用referencedString替换选定的字符。

但是没有出现。 对NSTextView没有任何改变。 如果我将NSTextStorage内容保存为webarchive:

    guard let store = textEditor.textStorage else { abort() }
    let attr: [String: AnyObject] = [NSDocumentTypeDocumentAttribute: NSWebArchiveTextDocumentType]
    let data = try store.dataFromRange(NSMakeRange(0, store.length), documentAttributes: attire)
    //... save 'data' to DB ...

然后重新加载它们:

    //... load 'data' from the DB ...
    guard let store = textEditor.textStorage else { abort() }
    let str = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSWebArchiveTextDocumentType], documentAttributes: nil)
    store.setAttributedString(str)

......图像恰好出现在正确的位置。

我很困惑这个。 有没有人对我失踪的东西有任何想法?

BTW - 我尝试使用以下方法强制刷新:

let frame = self.textEditor.frame
self.textEditor.setNeedsDisplayInRect(frame)

那没用。

建议您提供任何建议。 干杯。 保罗

编辑:附录......我一直在搞乱NSTextAttachmentCells,希望它们可以工作。他们以完全不同的方式失败。

如果我只是将上面的图像插入代码替换为:

    let cell = NSTextAttachmentCell(imageCell: theImage)
    let txtAtt = NSTextAttachment()
    txtAtt.attachmentCell = cell
    let str = NSAttributedString(attachment: txtAtt)
    let range = self.textEditor.selectedRange()
    store.replaceCharactersInRange(range, withAttributedString: str)

......然后,图像会在插入后立即显示,并且恰好位于正确的位置。 但他们没有得救! 也就是说,当我进行保存和重新加载时(根据上面的代码),它们不会包含在webarchive中。

编辑^ 2:因此,上面的明显结论是将一个图像附加到TextAttachment, 并将图像附加到NSTextAttachmentCell,该图像也附加到TextAttachment。 嗯......那不行。 但是这是什么工作:

    let cell = NSTextAttachmentCell(imageCell: theImage)
    let txtAtt = NSTextAttachment(data: theImage.TIFFRepresentation, ofType: kUTTypeTIFF as String)
    txtAtt.attachmentCell = cell
    //txtAtt.image = theImage
    let str = NSAttributedString(attachment: txtAtt)
    let rng = self.textEditor.selectedRange()
    store.replaceCharactersInRange(rng, withAttributedString: str)

那就是......我创建一个TextAttachment,传递图像的TIFF表示数据。 我还创建了AttachmentCell。 这现在有效 - 插入是可见的,并保存。

唯一的缺点是TIFF很大 - 图像扩展到内存大小的3倍。

任何人都可以提出一些想法,为什么这有效?

好的 - 这是解决方案。

NSTextAttachmentCell表示

...用于绘制文本附件图标并在其图标apple docs上处理鼠标事件的对象的界面。

因此,您需要使用图像创建其中一个。

NSTextAttachment

...包含NSData对象或NSFileWrapper对象,后者依次保存附加文件apple docs的内容

所以你需要两个 - 一个用于显示和交互,一个用于保存(即实际数据)。 保存TIFF(太大)时遇到的问题可以通过创建JPG或PNG表示来克服,这可以很好地用于NSData对象。 我通过扩展NSImage来做到这一点:

extension NSImage {
    var imagePNGRepresentation: NSData {
        return NSBitmapImageRep(data: TIFFRepresentation!)!.representationUsingType(.NSPNGFileType, properties: [:])!
    }
    var imageJPGRepresentation: NSData {
        return NSBitmapImageRep(data: TIFFRepresentation!)!.representationUsingType(.NSJPEGFileType, properties: [:])!
    }
}

(感谢来自这里的评论者如何做到这一点)。

所以这里的代码实际上可以将图像插入NSTextView(请滚筒)...

guard let theImage = NSImage(contentsOfURL: openPanel.URL!) else {
    showErrorMessage("Unable to load image")
    return
}
guard let store = self.textEditor.textStorage else { abort() }

let cell = NSTextAttachmentCell(imageCell: theImage)
let txtAtt = NSTextAttachment(data: theImage.imageJPGRepresentation, ofType: kUTTypeJPEG as String)
txtAtt.attachmentCell = cell
let str = NSAttributedString(attachment: txtAtt)
let range = self.textEditor.selectedRange()
store.replaceCharactersInRange(range, withAttributedString: str)

当我将其保存为webarchive时,我可以看到JPG对象与HTML一起保存。

扩展此...您可以插入任何数据类型(例如docs)并在Cell中使用图标表示。 凉。

奇怪的是,描述此内容的大多数网页都不包含Cell。 老实说我不知道​​那是怎么回事。

干杯。 保罗。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM