2012-07-12 26 views
1

我在iOS中使用UIKit来创建我用于布局预置文件的各种XIB文件的PDF文件。在iOS的单独线程中创建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生成涉及一些访问核心数据获取一些信息。

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

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

+0

很难说出这里发生了什么,更不用说问题可能了。你可以添加更多信息吗? – Caleb 2012-07-14 20:46:21

回答

1

如果您正在从XIB文件加载内容,请在主线程中加载该内容。

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