Tengo una aplicación de iOS antigua que usa UIKit y Objective C que actualmente estoy portando a SwiftUI y Swift. Todo iba muy bien y me encanta Swift y SwiftUI. La aplicación está prácticamente terminada, pero la aplicación depende de que el usuario pueda imprimir y / o guardar la vista principal como PDF. Simplemente no puedo averiguar cómo acceder a una vista en swiftui para convertirla en un PDF. Aquí está mi código objetivo-c existente / funcional.
- (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 es la vista que estoy ajustando y eventualmente convirtiendo a PDF. He logrado portar la mayor parte de este código hasta que llego a la línea 26 donde necesito renderizar la vista. Ni siquiera sé por dónde empezar para obtener una instancia de mi punto de vista.