[英]iOS Objective C - Creating UIViews with LongPress / Using multiple gestures on same UIViews
[英]IOS Objective C split a large UIView in to multiple UIViews and convert them into PDF
我有這段代碼將UIView
轉換為PDF
。 實際上,我正在做的是有兩個字符串數組,第一個數組保存問題字符串,第二個數組保存答案字符串。 我想打印整個對話,為此,我將UIView
轉換為PDF
。 但是問題是我正在根據問題答案字符串動態增加UIView
的大小。 因此,在對話結束時,視圖高度變得非常大。 現在,我需要將該視圖拆分為多個視圖以打印多頁PDF
。
我已經知道如何從多個UIViews
打印多頁PDF
,但是我正在努力的工作是如何將大視圖拆分為多個小A4標准尺寸視圖。
我還將創建的PDF
文件保存到document directory
但是我對該部分一無所知,這就是為什么我不在此函數中顯示該代碼的原因。
任何形式的幫助將不勝感激。 謝謝。
這是我的代碼:
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename {
NSMutableData* pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0.0f, 0.0f, 612.0f, 792.0f), nil);
int numberOfPages = ceil(aView.frame.size.height/792.0f);
for ( int i = 0 ; i < numberOfPages ; i++)
{
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[aView.layer renderInContext:pdfContext];
}
UIGraphicsEndPDFContext();
[self printItem:pdfData]; // I am using this function to print the pdf file
}
我在這里使用的代碼是重復打印同一視圖的多個頁面。
-(void) createPDF
{
NSString *pdfFilePath = [NSString stringWithFormat:@"%s",filePath];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:pdfFilePath]);
NSUInteger totalPages = CGPDFDocumentGetNumberOfPages(document);
UIGraphicsBeginPDFContextToFile(pdfFilePath, CGRectZero, nil);
for (int i = 1; i <= totalPages; i++)
{
CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, i);
CGRect cropBoxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);
CGRect mediaBoxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);
NSUInteger rotationAngle = 0;//;
int pageAngle = (CGPDFPageGetRotationAngle(pageRef) + rotationAngle) % 360;
NSInteger pageWidth = 0;
NSInteger pageHeight = 0;
switch (pageAngle) // Page rotation angle (in degrees)
{
default: // Default case
case 0: case 180: // 0 and 180 degrees
{
pageWidth = effectiveRect.size.width;
pageHeight = effectiveRect.size.height;
break;
}
case 90: case 270: // 90 and 270 degrees
{
pageWidth = effectiveRect.size.height;
pageHeight = effectiveRect.size.width;
break;
}
}
if (pageWidth % 2) pageWidth--;
if (pageHeight % 2) pageHeight--;
CGRect pageFrame = CGRectZero;
pageFrame.size = CGSizeMake(pageWidth, pageHeight);
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextTranslateCTM(context, 0.0f, -pageFrame.size.height);
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, pageFrame, (int)rotationAngle, true));
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextDrawPDFPage(context, pageRef);
CGContextRestoreGState(context);
CGContextTranslateCTM(context, pageWidth / 2, pageHeight / 2);
CGContextRotateCTM(context, degreesToRadians(rotationAngle));
CGFloat aspectRatio = (CGFloat)pageWidth / pageHeight;
if (rotationAngle == 90 || rotationAngle == 270)
{
CGContextTranslateCTM(context, - pageHeight / 2, - pageWidth / 2);
CGContextScaleCTM(context, 1 / aspectRatio, aspectRatio);
}
else
{
CGContextTranslateCTM(context, - pageWidth / 2, - pageHeight / 2);
}
UIImage *pageViewImage = [self getPageImageAtIndex:i];
[pageViewImage drawInRect:pageFrame];
}
UIGraphicsEndPDFContext();
CGPDFDocumentRelease(document);
}
-(UIImage *) getPageImageAtIndex:(int) index
{
// Design page view and take snapshot of view with fixed number of questions and answers on view and return the snapshot image from here.
int startIndex = 0;
int endIndex = 0;
if(i==1)
{
startIndex = 0; // start from 0 and draw upto 10
endIndex = 10;
}
else if(i==2)
{
startIndex = 11; // start from 11 and draw upto 20
endIndex = 20;
}
else if(i==3)
{
startIndex = 21; // start from 21 and draw upto 30
endIndex = 30;
}
.
.
.
.
for(int i= startIndex; i<= endIndex;i++)
{
// Drawing goes here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.