问题描述
我正在尝试使用UIDocumentInteractionController presentPreviewAnimated方法在iOS设备上显示pdf,但它会一直显示空白文档。我认为它可能与字符编码有关,但我不确定。如果我使用UIWebView,我可以显示pdf,而不是文档交互控制器。
I'm trying to display a pdf on iOS devices using the UIDocumentInteractionController presentPreviewAnimated method, but it keeps displaying a blank document. I think it might have to do with the character encoding, but I'm not sure. If I use a UIWebView, I can get the pdf to display, just not with the document interaction controller.
//更新9/18/14
这个现在正在使用Xcode 6的GM版本。
// UPDATE 9/18/14This is now working with the GM release of Xcode 6.
//更新2014年8月22日
// UPDATE 8/22/14
奇怪从DocumentInteractionController中,如果我点击右上角的打开方式图标并选择类似iBooks的内容,则pdf会正确显示。似乎只是预览不想在屏幕上显示它。
Oddly enough, from the DocumentInteractionController, if I tap on the "Open In" icon in the top right corner and choose something like iBooks, the pdf displays correctly. It seems as though it's just the preview that doesn't want to display it on the screen.
这是我的代码(在Swift中):
Here's my code (in Swift):
// data is coming in as NSISOLatin1StringEncoding
func displayPdfInUIDocumentInteractionController(data: NSData) {
let fileName = NSTemporaryDirectory().stringByAppendingPathComponent("myFile.pdf")
let url: NSURL! = NSURL(fileURLWithPath: fileName)
// this does not seem to make a difference
// let pdfString = NSString(data: data, encoding: NSISOLatin1StringEncoding)
// pdfString.writeToURL(url!, atomically: true, encoding: NSISOLatin1StringEncoding, error: nil)
data.writeToURL(url, atomically: true)
if url != nil {
let docController = UIDocumentInteractionController(URL: url)
docController.UTI = "com.adobe.pdf"
docController.delegate = self
docController.presentPreviewAnimated(true)
}
}
此代码确实正确显示pdf:
This code does display the pdf correctly:
// data is coming in as NSISOLatin1StringEncoding
func displayPdfInUIWebView(data: NSData) {
let rect = UIScreen.mainScreen().bounds
let screenSize = rect.size
let webView = UIWebView(frame: CGRectMake(0,0,screenSize.width,screenSize.height))
webView.autoresizesSubviews = true
webView.autoresizingMask = (UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth)
webView.loadData(data, MIMETYype: "application/pdf", textEncodingName: "ISO-8859-1", baseUrl: nil)
self.view.addSubview(webView)
}
有没有理由第一个函数不应该是加工?它不会出错,只显示一个空白页。
Is there any reason the first function should not be working? It doesn't error out, just displays a blank page.
推荐答案
这与Xcode的GM版本一起使用。猜猜这只是一个错误。
This is working with the GM release of Xcode. Guess it was just a bug.
这篇关于UIDocumentInteractionController显示空白pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!