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/