UIPrintInteractionController PresentFromRect 问题

发布于 2024-12-14 10:11:17 字数 2785 浏览 1 评论 0原文

我使用presentFromRect方法从UIButton显示AirPrint选项,一切都按预期工作,但如果我继续按足够快的按钮,我的应用程序会因EXC_BAD_ACCESS而崩溃,可能是因为弹出窗口没有被释放。

我使用的是启用了 ARC 的 Xcode 4.2。

任何帮助都会很棒!

更新2:该问题仅出现在iOS 5模拟器上,iPad 4.3模拟器按预期工作。

更新:

这里是真正的问题:

* 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“-[UIPopoverController dealloc] 在弹出窗口仍然可见时到达。”

这是代码:

这里我正在调用我的方法:

PrintUtils* printUtils = [[PrintUtils alloc] init];
printUtils.delegate = self;
[printUtils setHeader:@"Header"];
[printUtils print:self.webView fromRect:self.myButton.frame inView:self.menuView];

我的方法:

- (void)print:(UIWebView *)webView fromRect:(CGRect)rect inView:(UIView *)view
{

    printController = [UIPrintInteractionController sharedPrintController];
    [printController setDelegate:self];


    if(!printController){

        NSError* error = [[NSError alloc] initWithDomain:@"Print unavailable!" code:0 userInfo:nil];

        [self showError:error];
        return;
    }

    UIPrintInteractionCompletionHandler completionHandler = 
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if(!completed && error){
            [self showError:error];
        }
    };

    UIPrintInfo* printInfo = [UIPrintInfo printInfo];
    [printInfo setJobName:header];
    [printInfo setDuplex:UIPrintInfoDuplexLongEdge];
    [printInfo setOutputType:UIPrintInfoOutputGeneral];

    UIPrintFormatter* viewFormatter = [webView viewPrintFormatter];

    CustomPrintPageRenderer *pageRenderer = [[CustomPrintPageRenderer alloc] init];    
    [pageRenderer setJobTitle:[printInfo jobName]];

    UIFont* font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT]; 
    CGSize titleSize = [pageRenderer.jobTitle sizeWithFont:font];
    pageRenderer.headerHeight = pageRenderer.footerHeight = titleSize.height + HEADER_FOOTER_MARGIN_PADDING;


    [pageRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];

    [printController setPrintPageRenderer:pageRenderer];
    [printController setPrintInfo:printInfo];    
    [printController setPrintFormatter:viewFormatter];
    [printController setShowsPageRange:YES];    
    [printController presentFromRect:rect inView:view animated:YES completionHandler:completionHandler];

}

我的尝试修复实现 UIPrintInteractionControllerDelegate 方法的问题:

- (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
{

    if (visible) {
        [printController dismissAnimated:YES];
    }
}

Im showing the AirPrint options from a UIButton using the presentFromRect method, everything is working as expected, but if i keep pressing the button fast enough my app crashes with EXC_BAD_ACCESS, probably because of the popover not being release.

Im using Xcode 4.2 with ARC enabled.

Any help would be great!

Update 2: The problem only occur on iOS 5 simulator, iPad 4.3 simulator works as expected.

Update:

Here is the real problem:

* Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.'

Here is the code:

Here I'm calling my method:

PrintUtils* printUtils = [[PrintUtils alloc] init];
printUtils.delegate = self;
[printUtils setHeader:@"Header"];
[printUtils print:self.webView fromRect:self.myButton.frame inView:self.menuView];

My method:

- (void)print:(UIWebView *)webView fromRect:(CGRect)rect inView:(UIView *)view
{

    printController = [UIPrintInteractionController sharedPrintController];
    [printController setDelegate:self];


    if(!printController){

        NSError* error = [[NSError alloc] initWithDomain:@"Print unavailable!" code:0 userInfo:nil];

        [self showError:error];
        return;
    }

    UIPrintInteractionCompletionHandler completionHandler = 
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if(!completed && error){
            [self showError:error];
        }
    };

    UIPrintInfo* printInfo = [UIPrintInfo printInfo];
    [printInfo setJobName:header];
    [printInfo setDuplex:UIPrintInfoDuplexLongEdge];
    [printInfo setOutputType:UIPrintInfoOutputGeneral];

    UIPrintFormatter* viewFormatter = [webView viewPrintFormatter];

    CustomPrintPageRenderer *pageRenderer = [[CustomPrintPageRenderer alloc] init];    
    [pageRenderer setJobTitle:[printInfo jobName]];

    UIFont* font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT]; 
    CGSize titleSize = [pageRenderer.jobTitle sizeWithFont:font];
    pageRenderer.headerHeight = pageRenderer.footerHeight = titleSize.height + HEADER_FOOTER_MARGIN_PADDING;


    [pageRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];

    [printController setPrintPageRenderer:pageRenderer];
    [printController setPrintInfo:printInfo];    
    [printController setPrintFormatter:viewFormatter];
    [printController setShowsPageRange:YES];    
    [printController presentFromRect:rect inView:view animated:YES completionHandler:completionHandler];

}

My attempt to fix the problem implementing the UIPrintInteractionControllerDelegate Method:

- (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
{

    if (visible) {
        [printController dismissAnimated:YES];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相守太难 2024-12-21 10:11:17

你有没有弄清楚这一点?问题是您没有检查设备是 iPad 还是 iPhone。 iPhone 不支持presentFromRect:,但iPad 支持。这就是为什么当你在 iPhone 上运行它时会出现错误的访问。

您需要执行以下操作:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

    [printController presentFromFromBarButtonItem:sender animated:YES completionHandler:completionHandler];

} else {

    [printController presentAnimated:YES completionHandler:completionHandler];

}

Did you ever figure this out? The problem is that you´re not checking if the device is an iPad or iPhone. iPhone does not support presentFromRect: but iPad does. That's why you get the bad access when you run it on iPhone.

Here's what you need to do:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

    [printController presentFromFromBarButtonItem:sender animated:YES completionHandler:completionHandler];

} else {

    [printController presentAnimated:YES completionHandler:completionHandler];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文