Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Swift 4将图像附件插入macOS上的NSMutableAttributedString_Swift_Macos_Cocoa_Nsimage_Nstextattachment - Fatal编程技术网

使用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>"