1

在保存面板保存后调用以下函数,该应用程序基本上是将图像拼接成多张图片 - 这有效但是当沙盒它不起作用时,我相信它是不工作的保存线程 - 有谁知道这是为什么沙盒时不工作

- (void)saveThread{

    NSLog(@"Save thread started");
    @autoreleasepool { 
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:tileCutterView.filename];

    [rowBar setIndeterminate:NO];
    [columnBar setIndeterminate:NO];
    [rowBar setMaxValue:(double)[image rowsWithTileHeight:[heightTextField floatValue]]];
    [rowBar setMinValue:0.];
    [rowBar setDoubleValue:0.];
    [columnBar setMinValue:0.];
    [columnBar setMaxValue:(double)[image columnsWithTileWidth:[widthTextField floatValue]]];
    [columnBar setDoubleValue:0.];

    progressCol = 0;
    progressRow = 0;

    tileRowCount = [image rowsWithTileHeight:tileHeight];
    tileColCount = [image columnsWithTileWidth:tileWidth];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    TileCutterOutputPrefs outputFormat = (TileCutterOutputPrefs)[defaults integerForKey:@"OutputFormat"];
    for (int row = 0; row < tileRowCount; row++)
    {
        // Each row operation gets its own ImageRep to avoid contention
        NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:[image CGImageForProposedRect:NULL context:NULL hints:nil]];
        TileOperation *op = [[TileOperation alloc] init];
        op.row = row;
        op.tileWidth = tileWidth;
        op.tileHeight = tileHeight;
        op.imageRep = imageRep;
        op.baseFilename = baseFilename;
        op.delegate = self;
        op.outputFormat = outputFormat;
        [queue addOperation:op];
    }

}
}

更新:我添加了与此代码相关的其他功能:

- (IBAction)saveButtonPressed:(id)sender{
NSSavePanel *sp = [NSSavePanel savePanel];
[sp setRequiredFileType:@"jpg"];

   [sp beginSheetForDirectory:[NSString stringWithFormat:@"%@/Pictures", NSHomeDirectory()]
                      file:@"output.jpg" 
            modalForWindow:window
             modalDelegate:self 
            didEndSelector:@selector(didEndSaveSheet:returnCode:conextInfo:) 
               contextInfo:nil];
}





-(void)didEndSaveSheet:(NSSavePanel *)savePanel
        returnCode:(int)returnCode conextInfo:(void *)contextInfo
{
    if (returnCode == NSOKButton) 
    {
        NSURL *fileURL = [savePanel URL];
        NSString *filePath = [fileURL path];
        self.baseFilename = filePath;
        tileHeight = [heightTextField intValue];
        tileWidth = [widthTextField intValue];

        [self performSelector:@selector(delayPresentSheet) withObject:nil afterDelay:0.1];
}
}



- (void)delayPresentSheet{
    [progressLabel setStringValue:@"Splicing image…"];
    [rowBar setIndeterminate:YES];
    [columnBar setIndeterminate:YES];
    [rowBar startAnimation:self];
    [columnBar startAnimation:self];

    [NSApp beginSheet: progressWindow
   modalForWindow: window
    modalDelegate: self
   didEndSelector: @selector(didEndSheet:returnCode:contextInfo:)
      contextInfo: nil];

    [self performSelectorInBackground:@selector(saveThread) withObject:nil];
}

更新:控制台错误:

2015 年 6 月 5 日 11:30:49.592 沙盒 [8455]: ([8720]) MyApp(8720) 拒绝文件写入创建 /Users/Me/Documents/output_1_0.jpg

2015 年 6 月 5 日 11:30:49.592 沙盒 [8455]: ([8720]) MyApp(8720) 拒绝文件写入创建 /Users/Me/Documents/output_1_1.jpg

2015 年 6 月 5 日 11:30:49.592 沙盒 [8455]: ([8720]) MyApp(8720) 拒绝文件写入创建 /Users/Me/Documents/output_0_1.jpg

4

1 回答 1

0

NSSavePanel不能一次授予您访问多个文件的权限。您需要使用 aNSOpenPanel并要求用户授予对整个目录的访问权限。然后使用来自 NSOpenPanel 的 NSURL 并将您的文件添加到此。

您的其他问题的答案和此处的Apple 文档中有更多解释。

于 2015-05-07T12:49:44.867 回答