PDFKit 的 scaleFactorForSizeToFit 无法在 SwiftUI (UIViewRepresentable) 中设置缩放
PDFKit's scaleFactorForSizeToFit isn't working to set zoom in SwiftUI (UIViewRepresentable)
我正在开发一个使用 PDFKit 显示 PDF 的应用程序,我需要能够设置最小缩放级别 - 否则用户只能永远缩小。我已经尝试设置 minScaleFactor
和 maxScaleFactor
,因为它们会关闭 autoScales
,我需要将 scaleFactor
设置为 pdfView.scaleFactorForSizeToFit
。但是,此设置不会导致与 autoScales
相同的开始缩放,尽管更改了实际的 scaleFactor
数字,但开始缩放不会改变。这张照片与 autoScales
合影:
然后当我使用 scaleFactorForSizeToFit
时会发生什么:
引用scaleFactorForSizeToFit
的苹果文档,这是
"size to fit" scale factor that autoScales would use for scaling the current document and layout.
我已经在下面粘贴了我的代码。谢谢你的帮助。
import PDFKit
import SwiftUI
import Combine
class DataLoader : ObservableObject {
@Published var data : Data?
var cancellable : AnyCancellable?
func loadUrl(url: URL) {
cancellable = URLSession.shared.dataTaskPublisher(for: url)
.map { [=10=].data }
.receive(on: RunLoop.main)
.sink(receiveCompletion: { (completion) in
switch completion {
case .failure(let failureType):
print(failureType)
//handle potential errors here
case .finished:
break
}
}, receiveValue: { (data) in
self.data = data
})
}
}
struct PDFSwiftUIView : View {
@StateObject private var dataLoader = DataLoader()
var StringToBeLoaded: String
var body: some View {
VStack {
if let data = dataLoader.data {
PDFRepresentedView(data: data)
.navigationBarHidden(false)
} else {
CustomProgressView()
//.navigationBarHidden(true)
}
}.onAppear {
dataLoader.loadUrl(url: URL(string: StringToBeLoaded)!)
}
}
}
struct PDFRepresentedView: UIViewRepresentable {
typealias UIViewType = PDFView
let data: Data
let singlePage: Bool = false
func makeUIView(context _: UIViewRepresentableContext<PDFRepresentedView>) -> UIViewType {
let pdfView = PDFView()
// pdfView.autoScales = true
// pdfView.maxScaleFactor = 0.1
pdfView.minScaleFactor = 1
pdfView.scaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.maxScaleFactor = 10
if singlePage {
pdfView.displayMode = .singlePage
}
return pdfView
}
func updateUIView(_ pdfView: UIViewType, context: UIViewRepresentableContext<PDFRepresentedView>) {
pdfView.document = PDFDocument(data: data)
}
func canZoomIn() -> Bool {
return false
}
}
struct ContentV_Previews: PreviewProvider {
static var previews: some View {
PDFSwiftUIView(StringToBeLoaded: "https://www.campwinadu.com/wp-content/uploads/2022/03/Winadu-Monthly-Bugle-March-2022-1.pdf")
.previewInterfaceOrientation(.portrait)
}
}
可能跟顺序有关。这似乎对我有用:
pdfView.scaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.maxScaleFactor = 10.0
pdfView.minScaleFactor = 1.0
pdfView.autoScales = true
我最终能够解决这个问题。以下代码是我设法解决它的方法:
if let document = PDFDocument(data: data) {
pdfView.displayDirection = .vertical
pdfView.autoScales = true
pdfView.document = document
pdfView.setNeedsLayout()
pdfView.layoutIfNeeded()
pdfView.minScaleFactor = UIScreen.main.bounds.height * 0.00075
pdfView.maxScaleFactor = 5.0
}
出于某种原因,pdfView.scaleFactorForSizeToFit
不起作用 - 它总是 returns 0。这可能是一个 iOS 15 问题 - 我在另一个答案中注意到其他人有同样的问题。在上面的代码中,我所做的只是按屏幕高度缩放 PDF 以适合屏幕。这让我或多或少可以自己“自动缩放”。上面的代码正确地自动缩放 PDF 并防止用户缩小太多。
我正在开发一个使用 PDFKit 显示 PDF 的应用程序,我需要能够设置最小缩放级别 - 否则用户只能永远缩小。我已经尝试设置 minScaleFactor
和 maxScaleFactor
,因为它们会关闭 autoScales
,我需要将 scaleFactor
设置为 pdfView.scaleFactorForSizeToFit
。但是,此设置不会导致与 autoScales
相同的开始缩放,尽管更改了实际的 scaleFactor
数字,但开始缩放不会改变。这张照片与 autoScales
合影:
然后当我使用 scaleFactorForSizeToFit
时会发生什么:
引用scaleFactorForSizeToFit
的苹果文档,这是
"size to fit" scale factor that autoScales would use for scaling the current document and layout.
我已经在下面粘贴了我的代码。谢谢你的帮助。
import PDFKit
import SwiftUI
import Combine
class DataLoader : ObservableObject {
@Published var data : Data?
var cancellable : AnyCancellable?
func loadUrl(url: URL) {
cancellable = URLSession.shared.dataTaskPublisher(for: url)
.map { [=10=].data }
.receive(on: RunLoop.main)
.sink(receiveCompletion: { (completion) in
switch completion {
case .failure(let failureType):
print(failureType)
//handle potential errors here
case .finished:
break
}
}, receiveValue: { (data) in
self.data = data
})
}
}
struct PDFSwiftUIView : View {
@StateObject private var dataLoader = DataLoader()
var StringToBeLoaded: String
var body: some View {
VStack {
if let data = dataLoader.data {
PDFRepresentedView(data: data)
.navigationBarHidden(false)
} else {
CustomProgressView()
//.navigationBarHidden(true)
}
}.onAppear {
dataLoader.loadUrl(url: URL(string: StringToBeLoaded)!)
}
}
}
struct PDFRepresentedView: UIViewRepresentable {
typealias UIViewType = PDFView
let data: Data
let singlePage: Bool = false
func makeUIView(context _: UIViewRepresentableContext<PDFRepresentedView>) -> UIViewType {
let pdfView = PDFView()
// pdfView.autoScales = true
// pdfView.maxScaleFactor = 0.1
pdfView.minScaleFactor = 1
pdfView.scaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.maxScaleFactor = 10
if singlePage {
pdfView.displayMode = .singlePage
}
return pdfView
}
func updateUIView(_ pdfView: UIViewType, context: UIViewRepresentableContext<PDFRepresentedView>) {
pdfView.document = PDFDocument(data: data)
}
func canZoomIn() -> Bool {
return false
}
}
struct ContentV_Previews: PreviewProvider {
static var previews: some View {
PDFSwiftUIView(StringToBeLoaded: "https://www.campwinadu.com/wp-content/uploads/2022/03/Winadu-Monthly-Bugle-March-2022-1.pdf")
.previewInterfaceOrientation(.portrait)
}
}
可能跟顺序有关。这似乎对我有用:
pdfView.scaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.maxScaleFactor = 10.0
pdfView.minScaleFactor = 1.0
pdfView.autoScales = true
我最终能够解决这个问题。以下代码是我设法解决它的方法:
if let document = PDFDocument(data: data) {
pdfView.displayDirection = .vertical
pdfView.autoScales = true
pdfView.document = document
pdfView.setNeedsLayout()
pdfView.layoutIfNeeded()
pdfView.minScaleFactor = UIScreen.main.bounds.height * 0.00075
pdfView.maxScaleFactor = 5.0
}
出于某种原因,pdfView.scaleFactorForSizeToFit
不起作用 - 它总是 returns 0。这可能是一个 iOS 15 问题 - 我在另一个答案中注意到其他人有同样的问题。在上面的代码中,我所做的只是按屏幕高度缩放 PDF 以适合屏幕。这让我或多或少可以自己“自动缩放”。上面的代码正确地自动缩放 PDF 并防止用户缩小太多。