【问题标题】:NSImage loading from desktop getting nil in cocoa app从桌面加载的 NSImage 在可可应用程序中为零
【发布时间】:2019-03-03 17:00:18
【问题描述】:

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

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

testImage(null)的日志

上面的代码我正在使用,但测试图像变为零。

有什么建议吗?

提前致谢!

【问题讨论】:

  • imageURL的值是多少?
  • file:///Users/naresh/Desktop/Group%20495.png
  • 可能包含更多您的代码,或来自NSLogtestImage 的确切输出。
  • @I'L'I 更新了我的代码

标签: objective-c macos cocoa nsimage


【解决方案1】:

您可能在 Capabilities 中启用了 App Sandbox(我相信它是默认启用的)。启用后,如果没有用户交互(即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

【讨论】:

  • 我的应用在没有用户交互的情况下被沙盒化了,我们不能访问位于桌面的图像位置因为每次我不想展示 NSPanel 以及 HiddenMePresenter Mate 正在这样做他们在做什么?
  • 您将要使用安全范围的书签。我已经编辑了我的答案。
  • @iosdev 在沙盒应用程序中,如果没有任何用户交互,您无法访问容器外部的位置,没有办法
  • 不,他们甚至在第一次运行时也没有提示。
  • @vadian 检查 Hiddenme 应用程序他如何访问桌面壁纸?它是沙盒应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 2010-12-25
  • 2010-09-12
  • 1970-01-01
相关资源
最近更新 更多