【发布时间】:2014-12-03 00:54:49
【问题描述】:
我正在编写一个 OS X 沙盒应用程序。它接收@3x 和@2x 图像文件并将它们转换为较低分辨率的图像。我通过将文件拖到应用程序窗口或使用 NSOpenPanel 选择的目录来获取图像 URL。
拖动图片或选择文件夹后,我会运行 for 循环并使用此方法调整每个图片的大小
-(BOOL)writeImage:(NSImage*)image toFile:(NSString*)file withRepresentation:(NSString*)extension{
CGImageRef cgRef = [image CGImageForProposedRect:NULL
context:nil
hints:nil];
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef];
[newRep setSize:[image size]];
NSData *pngData = [newRep representationUsingType:[self getRepresentationType:extension] properties:nil];
return [pngData writeToFile:[file stringByAppendingString:[NSString stringWithFormat:@".%@",extension]] atomically:YES];
这打破了沙盒模型。我读过我应该使用 NSSavePanel,但这需要我为每个保存的图像指定最终文件路径/文件名,这会破坏用户体验。
有没有办法让沙盒应用程序允许写入某个目录而不是请求写入每个文件的权限?
【问题讨论】:
-
向搜索引擎询问安全范围的书签。
-
也许你有其他理由这样做,但你可以使用 automator 来批量调整图像大小...
标签: objective-c macos cocoa sandbox