Search code examples
iosobjective-cuitableviewuisplitviewcontrollerairprint

UISplitviewcontroller and printing "Airprint" with navigationbar


I got uisplitviewcontroller and it's working almost flawless how ever i implantent a way for th user to print using AirPrint... i had to implant it on every Detail storyboard how ever and make navigation bar not appear and then write in the AirPrint method. i wanted to know if theres anyway to do this properly in my seneario i lose the popupcontroller that navigates back to Index in the uitableview and i want to be able to have a "back" button and print from all Details i have

my AirPrint code :

- (void)printWebView:(id)sender {

UIGraphicsBeginImageContext(self.view.bounds.size);{
    CGPoint savedContentOffset = scrollView.contentOffset;
    CGRect savedFrame = scrollView.frame;

    scrollView.contentOffset = CGPointZero;
    scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);

    [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];

    scrollView.contentOffset = savedContentOffset;
    scrollView.frame = savedFrame;
}
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIPrintInteractionController *printer = [UIPrintInteractionController sharedPrintController];
printer.printingItem = viewImage;

UIPrintInfo *info = [UIPrintInfo printInfo];
info.orientation = UIPrintInfoOrientationLandscape;
info.outputType = UIPrintInfoOutputGeneral;
printer.printInfo = info;

UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error)
    NSLog(@"FAILED! due to error in domain %@ with error code %ld: %@",
          error.domain, (long)error.code, [error localizedDescription]);
};

if (IDIOM == IPAD)
[printer presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
else
[printer presentAnimated:YES completionHandler:completionHandler];
}

my navigation "sorta" can't set navigation bar to NO because then i won't print the current Detail and instead takes the first detail "Top"

-(void)mineSygedomme:(int)viewId {
UIViewController *viewController;
switch (viewId) {
    case 0:
        viewController = self.vaccinationer;
        [self.navigationController setNavigationBarHidden:YES animated:NO];
        break;
    case 1:
        viewController = self.skadestuen;
        [self.navigationController setNavigationBarHidden:YES animated:NO];
        break;
    case 2:
        viewController = self.leagen;
        [self.navigationController setNavigationBarHidden:YES animated:NO];
        break;
}
[self showChildViewController:viewController];
}
-(void)showViewWithId:(int)viewId {
UIViewController *viewController;
switch (viewId) {
    case 0:
        viewController = self.startSide;
        break;
}
[self showChildViewController:viewController];

}

i tried using this code on the other Details but it wouldn't work

#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Hovedmenu", @"Hovedmenu");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem

{

// Called when the view is shown again in the split view, invalidating the button and popover controller.
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}

i think i need a way to get current Detail/Storybord and then i can print from it but don't know how to do this


Solution

  • figured it out atlast ! had to change they way the AirPrint works. so it takes a screenshot of uiview and then prints that

    - (void)printWebView:(id)sender {
    
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGRect rect=self.view.frame;
    
    
    CGImageRef cropped1 = CGImageCreateWithImageInRect(viewImage.CGImage, rect);
    UIImage *imges =  [UIImage imageWithCGImage:cropped1];
    CGImageRelease(cropped1);
    // squarecropimg.image=imges;
    
    NSData *date=UIImagePNGRepresentation(imges);
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *doc = [paths objectAtIndex:0];
    NSString *appstr = [doc stringByAppendingPathComponent:@"currentview.png"];
    [date writeToFile:appstr atomically:YES];
    
    UIPrintInteractionController *printer = [UIPrintInteractionController sharedPrintController];
    printer.printingItem = viewImage;
    
    UIPrintInfo *info = [UIPrintInfo printInfo];
    info.orientation = UIPrintInfoOrientationLandscape;
    info.outputType = UIPrintInfoOutputGeneral;
    printer.printInfo = info;
    
    UIPrintInteractionCompletionHandler completionHandler =
    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
        if (!completed && error)
            NSLog(@"FAILED! due to error in domain %@ with error code %ld: %@",
                  error.domain, (long)error.code, [error localizedDescription]);
    };
    
    if (IDIOM == IPAD)
        [printer presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
    else
        [printer presentAnimated:YES completionHandler:completionHandler];
    }