我正在尝试裁剪,然后将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];
答案 0 :(得分:2)
你在代码的最初部分做了一些非常错误的事情......我甚至不想去那里,但让我分解一些事情:
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];