使用Swift 4将图像附件插入macOS上的NSMutableAttributedString
我想在使用Swift 4将图像附件插入macOS上的NSMutableAttributedString,swift,macos,cocoa,nsimage,nstextattachment,Swift,Macos,Cocoa,Nsimage,Nstextattachment,我想在macOS上的NSMutableAttributedString中插入图像附件。我已经看到了这在iOS上是如何工作的,但在macOS上我运气不太好。在iOS上,它可以如下所示: let string = NSMutableAttributedString(string: "Some great text") let attachment = NSTextAttachment() attachment.image = UIImage(named: "pic.png") let imageSt
macOS
上的NSMutableAttributedString
中插入图像附件。我已经看到了这在iOS
上是如何工作的,但在macOS
上我运气不太好。在iOS
上,它可以如下所示:
let string = NSMutableAttributedString(string: "Some great text")
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "pic.png")
let imageString = NSAttributedString(attachment: attachment)
string.append(imageString)
NSAttachment = "<NSTextAttachment: 0x6000000e3b00> \"pic.png\""
在macOS上,我正在尝试以下操作:
let string = NSMutableAttributedString(string: "yeah yeah yeah")
let attachment = NSTextAttachment()
attachment.image = NSImage(contentsOfFile: "<absolutePathTo>/pic.png")
let imageString = NSAttributedString(attachment: attachment)
string.append(imageString)
FileManager
说,给定我的路径,我的png存在。NSImage(contentsOfFile:“/pic.png”)返回图像吗?返回零。不确定原因。这是问题所在,与NSMutableAttributedString
无关。图像是否在应用程序包中?应用程序沙盒是否已打开?图像位于应用程序包之外。应用程序沙盒已打开,用户选择的文件已设置为读/写。我已将图像移动到我的下载目录中,并更新了允许从下载读取/写入的权限。代码现在可以工作了。我想我需要仔细阅读沙盒(我是XCode新手)。谢谢威勒克。
NSAttachment = "<NSTextAttachment: 0x6040000e4700>"