1

我在 iOS 中使用从用于假装文件布局的UIKit各种文件创建 PDF文件。XIB

我可以成功生成 PDF 文件。但是我在主线程中这样做,我想将它移动到一个单独的线程中,这样 UI 就不会“冻结”。

我曾尝试使用 GCD 这样做,但它不起作用,并且它抛出了一些错误。

我有这样的事情:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];

__block NSData *pdfNote = nil;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^{
    pdfNote = [self generatePdfFile];
    dispatch_sync(dispatch_get_main_queue(),^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
});

我看到的错误是:

failed to find PDF header: `%PDF' not found.
failed to find PDF header: `%PDF' not found.
failed to find PDF header: `%PDF' not found.
failed to find PDF header: `%PDF' not found.
2012-07-12 15:31:15.427 IrmaosPeixoto-CRM[7601:1a03] bool _WebTryThreadLock(bool), 0x3404d0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1   _ZL17_WebTryThreadLockb
2   WebThreadLock
3   -[UITextView setFrame:]
4   UIViewCommonInitWithFrame
5   -[UIView initWithCoder:]
6   -[UIScrollView initWithCoder:]
7   -[UITextView initWithCoder:]
8   UINibDecoderDecodeObjectForValue
9   UINibDecoderDecodeObjectForValue
10  -[UINibDecoder decodeObjectForKey:]
11  -[UIView initWithCoder:]
12  -[UIClassSwapper initWithCoder:]
13  UINibDecoderDecodeObjectForValue
14  -[UINibDecoder decodeObjectForKey:]
15  -[UIRuntimeConnection initWithCoder:]
16  UINibDecoderDecodeObjectForValue
17  UINibDecoderDecodeObjectForValue
18  -[UINibDecoder decodeObjectForKey:]
19  -[UINib instantiateWithOwner:options:]
20  -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:]
21  +[UIView(Extensions) loadFromNib:owner:]
22  -[FinishOrderViewController generatePdfFile]
23  __43-[FinishOrderViewController buttonPressed:]_block_invoke_0
24  _dispatch_call_block_and_release
25  _dispatch_worker_thread2
26  _pthread_wqthread
27  start_wqthread

附加说明:PDF 生成涉及对 Core Data 的一些访问以获取一些信息。

有没有更好的(工作)方式来实现我的需要?

编辑我弄错了,我使用UIKit而不是核心图形来生成 PDF。

4

1 回答 1

1

如果您要从 XIB 文件加载内容,请在主线程的包中加载元素。

只需在主线程中加载 NSBundle 中的元素,然后在另一个线程中运行所有内容,我就能够成功生成 PDF 文件。

于 2013-03-05T14:29:39.500 回答