UIPrintInteractionController的取消操作

37

我正在使用UIPrintInteractionController,并通过矩形来呈现它。

UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
// than set printing settings
...
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    [controller presentFromRect:rect inView:view animated:YES completionHandler:completionHandler];

然后我设置页面数量(>1)并选择打印机。 在设备旋转之前,我调用

[controller dismissAnimated:animated];

根据Xcode文档: 当以表单或从矩形动画呈现打印选项并且用户更改设备的方向时,应该关闭它们。

当我在旋转后呈现UIPrintInteractionController时,打印副本数将被设置回1(与初始视图中一样),而打印机仍然被选择。UIPrintInfo的Ivar _copies是私有的,所以我无法在旋转期间获取并存储它。

如何在旋转后恢复打印页数?


为什么你在旋转时将其忽略了? - NeverBe
4
因为苹果在 UIprintInteractionController 类的 dismissAnimated: 方法描述中建议这样做:“当打印选项以表单形式或从矩形动画显示且用户更改设备方向时,应该取消它们。” 和 “一旦新的方向生效,您应该再次呈现打印选项。” - Anastasia
1
这里是UIPrintInteractionController类的链接 - Anastasia
你最终找到了这个问题的答案吗? - Ryan Poolos
你是否在旋转时重新创建UIPrintInteractionController? - jjxtra
显示剩余2条评论
4个回答

1
[printInfo setValue:[NSNumber numberWithInteger:numberFile] forKey:@"copies"]

您可以设置和设置UIPrintInfo的@"copies"

1
抱歉,这可能是一个非常明显的问题,但您是否已将其作为委托进行调用?
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
controller.delegate = self;

-1
- (void)printImage:(id)sender {
  // Obtain the shared UIPrintInteractionController
  UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
  if(!controller){
    NSLog(@"Couldn't get shared UIPrintInteractionController!");
    return;
  }

  // We need a completion handler block for printing.
  UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    if(completed && error)
      NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
  };

      // Obtain a printInfo so that we can set our printing defaults.
    for(int i=0;i<[paths count];i++)
    {
        NSString *strFilePath = [paths objectAtIndex:i];
        NSLog(@"@ strFilePath is %@", strFilePath);
        NSData *data = [NSData dataWithContentsOfFile:strFilePath];
        if(data)
        {
            image = [UIImage imageWithData:data];
           // [arrayOfImages addObject:image];
               NSLog(@"@ image is %@", image);
            UIPrintInfo *printInfo = [UIPrintInfo printInfo];

            // This application prints photos. UIKit will pick a paper size and print
            // quality appropriate for this content type.
            printInfo.outputType = UIPrintInfoOutputPhoto;
            // The path to the image may or may not be a good name for our print job


            printInfo.jobName = @"PNg";
            NSLog(@"@ imageURL is %@",  printInfo.jobName);
            //  printInfo.jobName


            if(!controller.printingItems && image.size.width > image.size.height)
                printInfo.orientation = UIPrintInfoOrientationLandscape;

            // Use this printInfo for this print job.
            controller.printInfo = printInfo;


            controller.printingItems = nil;

        }
    }





#if DIRECT_SUBMISSION
  // Use the URL of the image asset.
    if(self.imageURL && [UIPrintInteractionController canPrintURL:self.imageURL])
      controller.printingItem = self.imageURL;
#endif

  // If we aren't doing direct submission of the image or for some reason we don't
  // have an ALAsset or URL for our image, we'll draw it instead.
  if(!controller.printingItems){
    // Create an instance of our PrintPhotoPageRenderer class for use as the
    // printPageRenderer for the print job.
    PrintPhotoPageRenderer *pageRenderer = [[PrintPhotoPageRenderer alloc]init];

    pageRenderer.imageToPrint = image;
    controller.printPageRenderer = pageRenderer;
    [pageRenderer release];
  }


  if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    [controller presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler];  // iPad
  }else
    [controller presentAnimated:YES completionHandler:completionHandler];  // iPhone

}

1
请提供更多的解释。你做了什么?为什么这样做?你的代码中关键的行在哪里? - TobiMcNamobi

-1
如果我理解正确,您真正需要的是如何在方向更改之前和之后运行代码:
在Swift中:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    //code goes here to run before orientation change 

    coordinator.animate(alongsideTransition: nil, completion: {
        _ in

        //code goes here to run after orientation change 
    })

}

Obj C文档:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接