从桌面加载NSImage在Cocoa应用程序中无效

时间:2018-09-27 18:04:16

标签: objective-c macos cocoa nsimage

嗨,我正在尝试加载位于桌面的图像。

NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]];
 NSLog(@"%@",imageURL);
 NSImage *testImage = [[NSImage alloc] initWithContentsOfURL:imageURL];
 NSLog(@"%@",testImage);
[self.desktopView setImage:testImage];

testImage (null)的日志

我正在使用的代码上方,但测试图像却没有显示。

有什么建议吗?

预先感谢!

1 个答案:

答案 0 :(得分:1)

您可能已在“功能”中启用了“应用程序沙箱”(我相信默认情况下已启用)。启用此功能后,如果没有用户互动(即NSOpenPanelNSSavePanel),您将无法访问您应用容器(或某些其他指定位置)之外的文件。

文件系统编程指南:https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

此处为应用沙盒文档:https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html

如果要禁用“应用程序沙箱”,可以通过单击项目文件>目标名称,选择功能选项卡并关闭应用程序沙箱将其关闭。

编辑:

您将要使用安全范围的书签来进行持久的资源访问。我对您评论中的应用程序不熟悉,但我猜它们会提示用户首次运行时需要访问。

此处的文档:

https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW16

https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW18

相关问题