gpt4 book ai didi

SwiftUI 打印 View

转载 作者:行者123 更新时间:2023-12-04 10:28:41 33 4
gpt4 key购买 nike

我有一个使用 UIKit 和 Objective C 的旧 iOS 应用程序,我目前正在将其移植到 SwiftUI 和 Swift。一切都很顺利,我喜欢 Swift 和 SwiftUI。该应用程序已经完成,但该应用程序依赖于用户能够打印和/或将主 View 保存为 PDF。我只是不知道如何访问 swiftui 中的 View 以将其转换为 PDF。这是我现有的/工作的objective-c 代码。

- (IBAction)actionPrint:(id)sender {
// CREATE CLEAR BACKGROUND
[legMain setBackgroundColor:[UIColor clearColor]];

// SCROLL TO BASE POSITION
[legMain scrollRectToVisible:CGRectMake(1, 1, 1, 1) animated:NO];

// RESET ZOOM
SnapPanel *myObject = [self fGetObject];
myObject.zoom = [NSNumber numberWithDouble:1.0];
[self fSave];

// RECORD FRAME SIZE AND SET TO CONTENT SIZE
double dWidth = legMain.frame.size.width;
double dHeight = legMain.frame.size.height;
[legMain setFrame:CGRectMake(legMain.frame.origin.x, legMain.frame.origin.y, legMain.contentSize.width, legMain.contentSize.height)];

// GET VIEW AS NSDATA FOR PDF
NSMutableData *pdfData = [NSMutableData data];
CGRect pageSize = CGRectMake(0.0, 0.0, 8.5 * 72.0, 11.0 * 72.0);
UIGraphicsBeginPDFContextToData(pdfData, pageSize, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();

// CREATE A SINGLE PAGE PDF
UIGraphicsBeginPDFPage();
[legMain.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();

// CREATE PRINT CONTROLLER
UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error){
NSLog(@"Print error: %@", error);
}
};

// SETUP PRINT CONTROLLER
[pc setShowsNumberOfCopies:YES];
[pc setShowsPageRange:YES];
[pc setShowsPaperSelectionForLoadedPapers:YES];
pc.printingItem = pdfData;

// DISPLAY CONTROLLER DIALOG
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[pc presentFromRect:texName.frame inView:viewMain.superview
animated:YES completionHandler:completionHandler];
} else {
[pc presentAnimated:YES completionHandler:completionHandler];
}

// RESET BACKGROUND COLOUR AND FRAME SIZE
[legMain setBackgroundColor:[UIColor colorWithRed:.95 green:.95 blue:.95 alpha:1.0]];
[legMain setFrame:CGRectMake(legMain.frame.origin.x, legMain.frame.origin.y, dWidth, dHeight)];
}

legMain 是我正在调整并最终转换为 PDF 的 View 。我已经设法移植了大部分代码,直到到达第 26 行,在那里我需要渲染 View 。我什至不知道从哪里开始获得我的观点的实例。

最佳答案

有权访问托管窗口,如 How to access own window within SwiftUI view? 中所述你可以得到主UIView从您的任何 SwiftUI View 中作为

let mainView = hostingWindow?.rootViewController.view

但是对于打印,我可能会使用如下专用 View (以避免影响 UI View )

if let hostingController = hostingWindow?.rootController as UIHostingController {
let printingView = UIHostingController(rootView: hostingController.rootView).view

// ... do anything with printingView here, because it will have
// a copy of SwiftUI rootView
}

更新:到评论中的详细信息(在这种情况下不需要托管窗口)
VStack {
PrintableView(arg: value) // << need to be made undependable, so all
// parameters should be passed via arguments
}

然后在打印操作上它可能就像

Button("Print") {
let printingView = UIHostingController(rootView: PrintableView(arg: value)).view
// do anything printing related with `printingView` here
}

关于SwiftUI 打印 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60517220/

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