简体   繁体   English

无法查看使用Alamofire下载的PDFView IOS中的PDF-SWIFT

[英]Cannot view PDF in PDFView IOS downloaded using Alamofire - SWIFT

I am trying to download a pdf file from the server and display it in PDFView in ios. 我正在尝试从服务器下载pdf文件,并在ios的PDFView中显示它。 I believe that I have successfully downloaded (file size is 1.1Mb) it and stored it in the DocumentsDirectory. 我相信我已经成功下载(文件大小为1.1Mb)并将其存储在DocumentsDirectory中。 However, when i try and display this pdf file in PDFView I see a blank viewcontroller. 但是,当我尝试在PDFView中显示此pdf文件时,我看到一个空白的viewcontroller。 Below is my code: 下面是我的代码:

I know that my UIView objects a properly connected and working. 我知道我的UIView对象正确连接并可以正常工作。 For instance, if I replace 'docURL' with the following file URL i can view a PDF file: 例如,如果我将“ docURL”替换为以下文件URL,则可以查看PDF文件:

 Bundle.main.url(forResource: "paper", withExtension: "pdf")

SWIFT: 迅速:

 let destination: DownloadRequest.DownloadFileDestination = { _, _ in
        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let fileURL = documentsURL.appendingPathComponent("pig.pdf")

        return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }


    Alamofire.download(containerPDFUrl, to: destination).response { response in
        print("response downLoadPDFDocument: \(response)")

        if response.error == nil, let pdfFilePath = response.destinationURL?.path {

            print("inside DispatchQueue")
            print("pdfFilePath: \(pdfFilePath)")

            DispatchQueue.main.async{
                // Instantiate PDFDocument
                let docURL = URL.init(string: pdfFilePath)
                let pdfDoc = PDFDocument.init(url: docURL!)
                self.myPDFView.document = pdfDoc

            }
        }else{
            print("response.error : \(response.error )")
        }
    }

Console: 安慰:

 response downLoadPDFDocument: DefaultDownloadResponse(request: Optional(https://myLink/rab1Mpub.pdf), response: Optional(<NSHTTPURLResponse: 0x60c0000302a0> { URL: https://myLink/rab1Mpub.pdf } { Status Code: 200, Headers {
"Accept-Ranges" =     (
    bytes
);
"Content-Length" =     (
    1113030
);
"Content-Type" =     (
    "application/octet-stream"
);
Date =     (
    "Sun, 18 Mar 2018 10:03:12 GMT"
);
Etag =     (
    "\"75b5f9132e45870cf1060c7ebfd0ed76\""
);
"Last-Modified" =     (
    "Mon, 12 Mar 2018 18:42:05 GMT"
);
Server =     (
    AmazonS3
);
"x-amz-id-2" =     (
    "KpynA2h+IMZKEk3ErsAAeIuvWOcuqBQMNiiwBV2K4j42TfC5z/ZQihvZFuHb7rHq0ESeKKeeD+s="
);
"x-amz-request-id" =     (
    F88529EC82881CD2
);
} }), temporaryURL: Optional(file:///Users/myName/Library/Developer/CoreSimulator/Devices/7EBBAD08-6C66-4108-AB24-DEA8892E207A/data/Containers/Data/Application/6BBA1B08-9F77-4509-952C-7F7528DAE855/tmp/CFNetworkDownload_h2MGIC.tmp), destinationURL: Optional(file:///Users/myName/Library/Developer/CoreSimulator/Devices/7EBBAD08-6C66-4108-AB24-DEA8892E207A/data/Containers/Data/Application/6BBA1B08-9F77-4509-952C-7F7528DAE855/Documents/pig.pdf), resumeData: nil, error: nil, timeline: Timeline: { "Request Start Time": 543060190.971, "Initial Response Time": 543060192.280, "Request Completed Time": 543060201.253, "Serialization Completed Time": 543060201.253, "Latency": 1.309 secs, "Request Duration": 10.281 secs, "Serialization Duration": 0.000 secs, "Total Duration": 10.281 secs }, _metrics: Optional((Task Interval) <_NSConcreteDateInterval: 0x608000227b00> (Start Date) 2018-03-18 10:03:10 +0000 + (Duration) 10.203282 seconds = (End Date) 2018-03-18 10:03:21 +0000
(Redirect Count) 0
(Transaction Metrics) (Request) <NSURLRequest: 0x608000008110> { URL: https://myLink/rab1Mpub.pdf }
(Response) <NSHTTPURLResponse: 0x608000227700> { URL: https://myLink/rab1Mpub.pdf } { Status Code: 200, Headers {
"Accept-Ranges" =     (
    bytes
);
"Content-Length" =     (
    1113030
);
"Content-Type" =     (
    "application/octet-stream"
);
Date =     (
    "Sun, 18 Mar 2018 10:03:12 GMT"
);
Etag =     (
    "\"75b5f9132e45870cf1060c7ebfd0ed76\""
);
"Last-Modified" =     (
    "Mon, 12 Mar 2018 18:42:05 GMT"
);
Server =     (
    AmazonS3
);
"x-amz-id-2" =     (
    "KpynA2h+IMZKEk3ErsAAeIuvWOcuqBQMNiiwBV2K4j42TfC5z/ZQihvZFuHb7rHq0ESeKKeeD+s="
);
"x-amz-request-id" =     (
    F88529EC82881CD2
);
} }
(Fetch Start) 2018-03-18 10:03:10 +0000
(Domain Lookup Start) 2018-03-18 10:03:10 +0000
(Domain Lookup End) 2018-03-18 10:03:11 +0000
(Connect Start) 2018-03-18 10:03:11 +0000
(Secure Connection Start) 2018-03-18 10:03:11 +0000
(Secure Connection End) 2018-03-18 10:03:11 +0000
(Connect End) 2018-03-18 10:03:11 +0000
(Request Start) 2018-03-18 10:03:11 +0000
(Request End) 2018-03-18 10:03:11 +0000
(Response Start) 2018-03-18 10:03:11 +0000
(Response End) 2018-03-18 10:03:21 +0000
(Protocol Name) http/1.1
(Proxy Connection) NO
(Reused Connection) NO
(Fetch Type) Network Load

))
inside DispatchQueue
pdfFilePath: 
/Users/myName/Library/Developer/CoreSimulator/Devices/7EBBAD08-6C66-4108-AB24-DEA8892E207A/data/Containers/Data/Application/6BBA1B08-9F77-4509-952C-7F7528DAE855/Documents/pig.pdf
2018-03-18 10:03:27.341067+0000 labbook[38709:4229832] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-03-18 10:04:06.253109+0000 labbook[38709:4231789] TIC Read Status [1:0x608000167800]: 1:57
2018-03-18 10:04:06.253340+0000 labbook[38709:4231789] TIC Read Status [1:0x608000167800]: 1:57
2018-03-18 10:04:06.253512+0000 labbook[38709:4231789] TIC Read Status [1:0x608000167800]: 1:57

PDFDocument(url: URL) only accepts a file URL , but in your code you say: PDFDocument(url: URL)仅接受文件URL ,但是在您的代码中您说:

let fileURL = URL.init(string: self.containerPDFUrl)
let pdfDoc = PDFDocument.init(url: fileURL!)

You say that self.containerPDFUrl is of the type 您说self.containerPDFUrl是这种类型

https://example.org/rab1Mpub.pdf https://example.org/rab1Mpub.pdf

But that is not a file url. 但这不是文件网址。 I believe you intended: 我相信您打算:

let pdfDoc = PDFDocument(url: self.destination.0)

But let fileURL = documentsURL.appendingPathComponent("pig.png") should likely be let fileURL = documentsURL.appendingPathComponent("pig.pdf") 但是应该let fileURL = documentsURL.appendingPathComponent("pig.png")应该是let fileURL = documentsURL.appendingPathComponent("pig.pdf")

Update 更新资料

if response.error == nil, let pdfFilePath = response.destinationURL?.path {

        print("inside DispatchQueue")
        print("pdfFilePath: \(pdfFilePath)")

        DispatchQueue.main.async{
            // Instantiate PDFDocument
            let docURL = URL.init(string: pdfFilePath)
            let pdfDoc = PDFDocument.init(url: docURL!)
            self.myPDFView.document = pdfDoc

        }
 }

There's no need to deal with paths here. 此处无需处理路径。 Simply take the destinationURL and init the document from that. 只需获取destinationURL并从中初始化文档。 For example: 例如:

if response.error == nil, let pdfURL = response.destinationURL? {

        DispatchQueue.main.async{
            // Instantiate PDFDocument
            let pdfDoc = PDFDocument(url: pdfURL)
            self.myPDFView.document = pdfDoc

        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM