我目前正在开发一个适用于 PDF 的 Cocoa 应用程序,并且正在使用 Apple 的 PDFKit 来完成这项工作。保存 PDF 被证明是一个问题,因为我想在发生这种情况时显示一个进度条,这似乎使用标准writeToURL:
方法是不可能的。所以,我去使用了 Grand Central Dispatch。
这样做的问题是,dataRepresentation
用于写入 NSData 的方法在将任何大于 ~3MB 的 PDF 转换为 NSData 时非常慢,因此当我的程序等待数据时,进度条会停顿几秒钟,这似乎使用户认为程序已完全停止。我真的不希望他们这么想。
我的问题是,我能做些什么来加快该dataRepresentation
方法的速度,或向用户报告其进度吗?
这是我最终编写的 Grand Central Dispatch 代码:
NSData *pdf = [doc dataRepresentation]; // R-e-a-l-l-y S-l-o-w
dispatch_queue_t queue = dispatch_get_current_queue();
dispatch_data_t dat = dispatch_data_create([pdf bytes], [pdf length], queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
__block dispatch_io_t channel =
dispatch_io_create_with_path(DISPATCH_IO_STREAM,
[path UTF8String], // Convert to C-string
O_WRONLY, // Open for writing
0, // No extra flags
queue,
^(int error){
// Cleanup code for normal channel operation.
// Assumes that dispatch_io_close was called elsewhere.
if (error == 0) {
dispatch_release(channel);
channel = NULL;
}
});
// The next two lines make sure that the block in dispatch_io_write
// gets called about 60 times, so that it can update the progress bar.
dispatch_io_set_low_water(channel,[pdf length]/60);
dispatch_io_set_high_water(channel,[pdf length]/60);
dispatch_io_write(channel,
0,
dat,
queue,
^(bool done, dispatch_data_t data, int error) {
if (data) {
// Increment progress bar
}
if (error) {
// Handle errors
}
if (done) {
dispatch_io_close(channel, NULL);
}
});
dispatch_release(dat);
dispatch_release(queue);