【问题标题】:issue in setting UIViews, view.bounds.origin.y while using in layer renderInContext:在层 renderInContext 中使用时设置 UIViews、view.bounds.origin.y 的问题:
【发布时间】:2011-08-10 10:02:50
【问题描述】:

我正在尝试裁剪 UIView,然后将其转换为 pdf 文件。 UIView 正确裁剪 x 组件、宽度和高度。但它采用相同的 y 分量,即 0 进行渲染。我想从顶部裁剪图像 110 点。这是我的代码

UIView *tempV;
    tempV=self.view;
    CGRect fram= tempV.bounds;
    fram.origin.x=537;
    fram.origin.y=110;
    fram.size.width=404;
    fram.size.height=772;
    tempV.bounds=fram;
    NSLog(@"Mail");
    NSLog(@"%f,%f,%f,%f",tempV.bounds.origin.x,tempV.bounds.origin.y,tempV.bounds.size.width,tempV.bounds.size.height);
    NSMutableData *pdfData=[NSMutableData data];
    UIGraphicsBeginPDFContextToData(pdfData, tempV.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext= UIGraphicsGetCurrentContext();
    [tempV.layer renderInContext:pdfContext];
    UIGraphicsEndPDFContext();
    MFMailComposeViewController *mailComposer=[[[MFMailComposeViewController alloc]init] autorelease];
    mailComposer.mailComposeDelegate=self;
    [mailComposer addAttachmentData: pdfData mimeType: @"application/pdf" fileName: @"Dudel creation.pdf"];
    [pdfData writeToFile:@"Dudel creation.pdf" atomically:YES];
    [self presentModalViewController: mailComposer animated: YES];

【问题讨论】:

    标签: objective-c ipad pdf uiview


    【解决方案1】:

    您在代码的初始部分中做了一些非常错误的事情......我什至不想去那里,但让我分解一些事情:

    1) UIGraphicsBeginPDFContextToData 第二个参数是CGRect

    2) 据我了解,您需要一个非常具体的矩形来显示屏幕上显示的内容,尽管它的中心与您的视图控制器的视图完全不同(您正在尝试更改原点和大小)。那么,为什么要在视图控制器的视图边界上创建依赖项呢? (记住边界和中心总是齐头并进的)。

    3) 那么为什么不直接去掉代码的初始部分并这样做:

    CGRect fram = CGRectMake (537, 110, 404, 772); // A rectangle with no other dependency, since you want one very specific.
    
    NSMutableData *pdfData=[NSMutableData data];
    UIGraphicsBeginPDFContextToData(pdfData, fram, nil); // Passing the newly created rectangle as the second parameter to the function.
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext= UIGraphicsGetCurrentContext();
    [tempV.layer renderInContext:pdfContext];
    UIGraphicsEndPDFContext();
    MFMailComposeViewController *mailComposer=[[[MFMailComposeViewController alloc]init] autorelease];
    mailComposer.mailComposeDelegate=self;
    [mailComposer addAttachmentData: pdfData mimeType: @"application/pdf" fileName: @"Dudel creation.pdf"];
    [pdfData writeToFile:@"Dudel creation.pdf" atomically:YES];
    [self presentModalViewController: mailComposer animated: YES];
    

    【讨论】:

    • 谢谢miamk,你解决了我的问题,尽管我在关闭这个函数时重建了边界,所以你上面提到的问题在一定程度上得到了解决。但这当然是一个糟糕的代码。我面临的问题是我无法像我想的那样使矩形从点 (537,110) 开始,但是我通过将 (537,-110) 提供给代码来得到它。再次感谢您。
    猜你喜欢
    • 2012-04-25
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多