在iOS中的单独线程中创建PDF文件

时间:2012-07-12 14:34:20

标签: ios uikit

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

我可以成功生成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。

1 个答案:

答案 0 :(得分:1)

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

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

相关问题