【问题标题】:Delete file after printing打印后删除文件
【发布时间】:2013-01-18 06:27:20
【问题描述】:

我试图在打印后删除一个文件,但没有成功。

实际上,我需要打印一个临时生成的 PDF(带有文本和图像),然后,当打印过程完成后,我想将其删除。

目前 PDF 保存在 Documents 目录中。将其保存在 Temp 文件夹中是个好主意吗?但无论如何我都必须删除它。 我还有一个 tableView 显示用户保存的 PDF,所以我只需要显示那些(而不是那些 临时生成的)

我尝试过使用 UIPrinterInteractionController 的委托方法,但没有任何运气。

-(void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController{
    NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory
                          stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",PDFNameString]];
    if ([fileMgr removeItemAtPath:filePath error:&error] != YES)
    {   UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:NSLocalizedString(@"UnableToDeleteFile", @"Unable to delete file: %@"),[error localizedDescription]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease];
        [alert show];
    }

    NSLog(@"Dismissed");
}

仅当打印选项视图离开“发送到打印机..”视图时,警报视图才会弹出。 它甚至没有删除文件,它说

Cocoa 错误代码 4.

有谁知道打印完成后我可以使用什么方法来删除文件?

编辑

如果文件存在,我 NSLog'd,但它不存在。这怎么可能?

【问题讨论】:

  • 是否调用了委托方法?如果是这样,您在尝试删除文件时遇到什么错误?
  • 是的,实际上弹出了alertView! (并且带有“Dismiss”的NSLog也出现了)。错误是(可可错误4。)
  • 我认为这个委托方法应该在您关闭弹出窗口后调用。所以问题是 - 您发布的代码是否有机会运行?
  • 是的!在我从“打印选项”屏幕中选择“打印”后不久,将显示警报视图,并且控制台显示“已关闭”。但是有一个视图显示“正在 [打印机名称] 上打印”,然后它被关闭了
  • 不是错误,但为什么不是:if (![fileMgr removeItemAtPath:filePath error:&error]) 而不是 if ([fileMgr removeItemAtPath:filePath error:&error] != YES)

标签: ios objective-c pdf printing


【解决方案1】:

【讨论】:

  • 试过这个,成功了。我的错误是我错过了 [NSString stringWithFormat:@"%@.pdf",PDFNameString]; 中的 .pdf谢谢大家!
猜你喜欢
  • 2011-05-27
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2017-08-21
  • 2018-11-20
  • 1970-01-01
相关资源
最近更新 更多