ios - 尝试加载共享 pdf 时出现错误 : Value of type 'UIView' has no member 'view' ,

标签 ios swift

STORYBOARD VIEW这就是 View Controller 的样子 在 UIView 扩展的方法中,我收到错误:

UIView has no member view

错误上线:

activityViewController.popoverPresentationController?.sourceView = self.view
import Foundation
import UIKit

extension ViewController {
 // Export pdf from Save pdf in drectory and return pdf file path
    func exportAsPdfFromView() -> String {
        getDirectoryPath()
        getImage()
        let layer = CAGradientLayer()
        layer.colors = [
            UIColor.red.cgColor,
            UIColor.green.cgColor
        ]

        //bounds = CGRect(x: originalBounds.origin.x, y: bounds.origin.y, width: self.bounds.size.width, height: self.scrollView.contentSize.height)
        let pdfPageFrame = view.bounds
        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, pdfPageFrame, nil)
        UIGraphicsBeginPDFPageWithInfo(pdfPageFrame, nil)
        guard let pdfContext = UIGraphicsGetCurrentContext() else { return "" }
        layer.render(in: pdfContext)
        UIGraphicsEndPDFContext()
        return self.saveViewPdf(data: pdfData)


    }

    // Save pdf file in document directory
    func saveViewPdf(data: NSMutableData) -> String {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let docDirectoryPath = paths[0]
        print(docDirectoryPath)
        let pdfPath = docDirectoryPath.appendingPathComponent("viewPdf.pdf")

        if data.write(to: pdfPath, atomically: true) {
            return pdfPath.path
            print("PDF written at path \(pdfPath.path)")
           // let data = try? Data(contentsOf: pdfPath!)
            //let str = String(bytes: data!, encoding: .utf8)
        } else {
            return ""
        }
    }
    func getImage(){
        let fileManager = FileManager.default
        let imagePAth = (self.getDirectoryPath() as NSString).appendingPathComponent("viewPdf.pdf")
        if fileManager.fileExists(atPath: imagePAth){
            print("HI")
            print(imagePAth)
        //imageView.image = UIImage(contentsOfFile: imagePAth)
            if fileManager.fileExists(atPath: imagePAth){
                let documento = NSData(contentsOfFile: imagePAth)
                let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [documento!], applicationActivities: nil)
                activityViewController.popoverPresentationController?.sourceView = self.view
                .presentViewController(activityViewController, animated: true, completion: nil)
            }
            else {
                print("document was not found")
            }
        }else{
            print("No Image")
        }
    }
}

最佳答案

你不能present来自 UIView 对象的 UIActivityViewController。创建一个像这样的 UIViewController extension 方法并从任何 View Controller 调用它

extension UIViewController {
    func getImage(){
        let fileManager = FileManager.default
        let imagePAth = (self.getDirectoryPath() as NSString).appendingPathComponent("viewPdf.pdf")
        if fileManager.fileExists(atPath: imagePAth){
            print("HI")
            print(imagePAth)
            //imageView.image = UIImage(contentsOfFile: imagePAth)
            if fileManager.fileExists(atPath: imagePAth) {
                let documento = NSData(contentsOfFile: imagePAth)
                let activityViewController = UIActivityViewController(activityItems: [documento!], applicationActivities: nil)
                activityViewController.popoverPresentationController?.sourceView = self.view
                self.present(activityViewController, animated: true, completion: nil)
            }
            else {
                print("document was not found")
            }
        }else{
            print("No Image")
        }
    }
    func getDirectoryPath() -> String {
        ///
        return string
    }
}

关于ios - 尝试加载共享 pdf 时出现错误 : Value of type 'UIView' has no member 'view' ,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56086093/

相关文章:

ios - Xcode4:构建 IPA 的替代方法

ios - swift Realm 列表<T> 未填充

ios - Pickerview 内容未显示

ios - 从不同的文本字段在 [RETURN] 按键上执行不同的功能

ios - 打开超过 6 个并行线程时获取错误代码 504 网关超时 NSURLConnection.sendSynchronousRequest

ios - 如何直接从浏览器安装 iOS 应用程序?

ios - SQlite 在 iOS App 中插入数据

arrays - Swift 中的 elementsEqual 和 '==' 有什么区别?

ios - Web View 在 Swift 2.0 中不显示内容

iphone - 使用ARMV7将目标设备限制为iPhone 4