gpt4 book ai didi

ios - Swift:DiskImageCache:无法解析旧目录的绝对路径

转载 作者:行者123 更新时间:2023-11-29 02:23:23 24 4
gpt4 key购买 nike

我正在尝试在我正在构建的应用程序的 View 中加载本地 PDF。我已将 PDF 加载到我的 images.cassetes 文件夹中。我还进入了“目标”>“信息”>“文档类型”,并添加了一个 PDF 作为文档类型:com.adobe.pdf。

我使用的是 Xcode 6.1.1 和 iOS 8。

这是我的 View Controller 的代码:

 import UIKit

class DisplayFirstStepsViewController: UIViewController {

@IBOutlet weak var currentDocument: UIWebView!

var currentFirstSteps : FirstStep?

override func viewDidLoad() {
super.viewDidLoad()

var pdfLoc = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource((named: currentFirstSteps!.filename), ofType:"pdf")!)
var request = NSURLRequest(URL: pdfLoc!);



self.currentDocument.loadRequest(request);


navigationController?.hidesBarsOnSwipe = true

// Do any additional setup after loading the view.
}

它在模拟器中有效,但在实际手机上无效。然后我单击“产品”>“清理”,现在出现此错误:

fatal error :在解包可选值时意外发现 nil

我看到使用此代码修复了原始错误:

NSString *path = [[NSBundle mainBundle] pathForResource:@"b777normalprocedures"     ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:targetURL];
_webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

但是,我不确定如何将其从 Objective-c 转换为 swift 或如何清理我的代码以按原样工作。感谢任何和所有的帮助!

最佳答案

func loadUrl(request:NSURL){
currentDocument.loadRequest(NSURLRequest(URL: request))
}
func loadData(data:NSData){
currentDocument.loadData(data, MIMEType: "application/pdf", textEncodingName: "utf-8", baseURL: nil)
}
if let pdfFileBundleUrl = NSBundle.mainBundle().URLForResource("pdfFileName", withExtension: "pdf") {
loadUrl(pdfFileBundleUrl)
}

if let targetURL = NSBundle.mainBundle().URLForResource("b777normalprocedures", withExtension: "pdf") {
if let pdfData = NSData(contentsOfURL:targetURL) {
loadData(pdfData)
println("Successfully loaded data" )
} else {
println("Failed to loading data")
}
}


let docsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL
docsUrl.absoluteURL!
docsUrl.absoluteString!
docsUrl.path!

代码示例

您还可以让它缓存 pdf,这样它就不会下载两次,如下所示:

首先将这两个变量添加到您的 AppDelegate.swift

//
// AppDelegate.swift
// pdfSample

import UIKit
var loaded = false
var cachedData:NSData? = nil

//

import UIKit
class ViewController: UIViewController {
@IBOutlet weak var wv: UIWebView!

let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL

func loadData(data:NSData){
cachedData = data
wv.loadData(data, MIMEType: "application/pdf", textEncodingName: "utf-8", baseURL: nil)

}
func loadPDF(link:String){
if let pdfOnlineUrl = NSURL(string: link) {
if let downloadedData = NSData(contentsOfURL: pdfOnlineUrl) {
let newFolderUrl = documentsUrl.URLByAppendingPathComponent("pdfs")
var err: NSErrorPointer = nil
if NSFileManager.defaultManager().createDirectoryAtPath(newFolderUrl.path!, withIntermediateDirectories: true, attributes: nil, error: err) {
println("directory successfully created and/or opened")
let pdfDestinationUrl = newFolderUrl.URLByAppendingPathComponent(pdfOnlineUrl.lastPathComponent!)
if downloadedData.writeToURL(pdfDestinationUrl, atomically: true) {
println("pdf saved")
if let data = NSData(contentsOfURL: pdfDestinationUrl) {
loadData(data)
let pdfFile = pdfDestinationUrl.lastPathComponent?.stringByDeletingPathExtension
println("Successfully loaded data pdf file named " + pdfFile! )
loaded = true
} else {
println("Failed to load data pdf file: \(pdfDestinationUrl.lastPathComponent?.stringByDeletingPathExtension)")
}
} else {
println("file not saved")
}
} else {
println("error creating destination folder / destination folder not found")
}
} else {
println("Failed to download font from URL: " + pdfOnlineUrl.description)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
if !loaded {
loadPDF("https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/SwiftStandardLibraryReference.pdf")
} else {
loadData(cachedData!)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

关于ios - Swift:DiskImageCache:无法解析旧目录的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com