在沙箱中读取 osx 截图图像 [英] Reading osx screenshot image in sandbox
问题描述
我想获取由不同 osx 快捷方式触发的屏幕截图.所以我用 kMDItemIsScreenCapture
添加了一个观察者.以下代码用于添加观察者.
I want to get the screenshot triggered by different osx shortcuts. So i add an observer with kMDItemIsScreenCapture
. Following code was used to add observer.
_query = [[NSMetadataQuery alloc] init];
[_query setDelegate:self];
[_query setPredicate:[NSPredicate predicateWithFormat:@"kMDItemIsScreenCapture = 1"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenshotQueryUpdated:) name:NSMetadataQueryDidUpdateNotification object:_query];
[_query startQuery];
screenshotQueryUpdated
的实现如下:
NSMetadataItem *item = [[notification.userInfo objectForKey:(NSString *)kMDQueryUpdateAddedItems] lastObject];
if (item) {
NSString *screenShotPath = [item valueForAttribute:NSMetadataItemPathKey];
NSData* temp = [NSData dataWithContentsOfFile:screenShotPath];
// More code....
}
问题是我无法在 sandbox
模式下在 screenShotPath 读取文件.那么使用沙箱在应用程序中获取屏幕截图文件的正确方法是什么.
Problem is i can not read file at screenShotPath in sandbox
mode. So what is the right way to get screenshot file in application with sandbox.
推荐答案
使用 NSOpenPanel
请求用户访问此文件或包含屏幕截图的文件夹.然后您将能够创建一个 安全范围书签,以便在您的应用后续启动时访问相同的资源.
Use an NSOpenPanel
to ask the user for access to either this file or the folder containing the screenshots. You will be able to then create a security scoped bookmark to get access on the same resource on subsequent launches of your app.
这篇关于在沙箱中读取 osx 截图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!