gpt4 book ai didi

iphone - 从 UIScrollView + iphone 生成 PDF

转载 作者:行者123 更新时间:2023-12-03 19:29:49 24 4
gpt4 key购买 nike

我想生成一个UIScrollView的pdf,其内容大小接近320*2000。如果我使用当前图形图像上下文来捕获 ScrollView 层,那么它仅捕获 ScrollView 的可见部分,而不是该 ScrollView 的整个层。

我正在使用以下代码:

-(void)CreatePdf:(id)sender
{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = @"myPDF.pdf";
[self writePDFAma];
CGRect arect=CGRectMake(0, 0, 768, 1024);

CreatePDFFileAma(arect, nil);
}



-(void) writePDFAma
{
UIImage *image;
NSData *data;
UIAlertView *successAlert;
UIAlertView *failureAlert;
//
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// // This should be our documents directory
NSString *saveDiirectory = [documentPath objectAtIndex:0];
// // Our PDF is named 'Example.pdf'
NSString *saveFileName = @"FormImage2.JPG";
// // Create the full path using our saveDirectory and saveFileName
NSString *finalPath = [saveDiirectory stringByAppendingPathComponent:saveFileName];
//NSLog(@"%@",finalPath);
CGSize asize=CGSizeMake(_scrollview.frame.size.width, _scrollview.frame.size.height);
//NSLog(@"%d..%d",mainViewAma.frame.size.width, mainViewAma.frame.size.height);
UIGraphicsBeginImageContext(asize);
[[_scrollview layer] renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

data = UIImageJPEGRepresentation(image, 1.0);

BOOL catch;
if(!catch){
if ([[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil])
{
successAlert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Doodle was successfully saved to the Photo Library." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
//[successAlert show];
[successAlert release];
} else {
failureAlert = [[UIAlertView alloc] initWithTitle:@"Failure" message:@"Failed to save doodle to the Photo Library." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
//[failureAlert show];
[failureAlert release];
}

}

}


void CreatePDFFileAma (CGRect pageRect, const char *filename)
{
// This code block sets up our PDF Context so that we can draw to it
//some code here
CGContextRef pdfContext;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// // This should be our documents directory
NSString *saveDirectory = [documentPath objectAtIndex:0];
// // Our PDF is named 'Example.pdf'



//NSString *saveFileName = @"PDFForm2.pdf";



// // Create the full path using our saveDirectory and saveFileName
NSString *finalPath = [saveDirectory stringByAppendingPathComponent:saveFileName];
NSURL * aurl=[[NSURL alloc]initFileURLWithPath:finalPath];
url=(CFURLRef)aurl;
// This dictionary contains extra options mostly for 'signing' the PDF
myDictionary = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("AMA FORM PDF"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name2"));
// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
// Cleanup our mess
CFRelease(myDictionary);
CFRelease(url);
// Starts our first page
CGContextBeginPage (pdfContext, &pageRect);

// Draws a black rectangle around the page inset by 50 on all sides
CGContextStrokeRect(pdfContext, CGRectMake(50, 50, 768, 1024));

// This code block will create an image that we then draw to the page
CGImageRef image;
CGDataProviderRef provider;
CFURLRef pictureURL;
documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// // This should be our documents directory
saveDirectory = [documentPath objectAtIndex:0];
NSString *saveFileName2 = @"FormImage2.JPG";
// // Create the full path using our saveDirectory and saveFileName
NSString *finalPath2 = [saveDirectory stringByAppendingPathComponent:saveFileName2];
NSURL * aurl2=[[NSURL alloc]initFileURLWithPath:finalPath2];
pictureURL=(CFURLRef)aurl2;
provider = CGDataProviderCreateWithURL (pictureURL);
CFRelease (pictureURL);
image = CGImageCreateWithJPEGDataProvider(provider, NULL, TRUE, kCGRenderingIntentDefault); //DataProvider (, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease (provider);
CGContextDrawImage (pdfContext, CGRectMake(0, 0,768, 1024),image);
CGImageRelease (image);
CGContextEndPage (pdfContext);
// We are done with our context now, so we release it
CGContextRelease (pdfContext);
}

最佳答案

嘿伙计们,我使用网上可用的示例准备了一些代码库,以从 UIScrollView 创建 PDF我们应该清楚两件事

1) number of pages 2) contentsize of scrollview

我创建了一个内容大小(10 * 910)的 ScrollView 。所以它有 10 页。。这是针对 iPad 的。

这是代码库

- (void) createPDF
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *directroyPath = nil;
directroyPath = [documentsDirectory stringByAppendingPathComponent:@"PDF"];
NSString *filePath = [directroyPath stringByAppendingPathComponent:@"test.pdf"];
// check for the "PDF" directory
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

} else {
[[NSFileManager defaultManager] createDirectoryAtPath:directroyPath
withIntermediateDirectories:NO
attributes:nil
error:&error];
}

CGContextRef pdfContext = [self createPDFContext:myScrollView.bounds path:(CFStringRef)filePath];
NSLog(@"PDF Context created");

for (int i = 0 ; i< 10 ; i++)
{

// page 1
CGContextBeginPage (pdfContext,nil);

//turn PDF upsidedown
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0, (i+1) * 910);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(pdfContext, transform);

//Draw view into PDF
[myScrollView.layer renderInContext:pdfContext];
CGContextEndPage (pdfContext);
[myScrollView setContentOffset:CGPointMake(0, (i+1) * 910) animated:NO];

}
CGContextRelease (pdfContext);
}

- (CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path
{
CGContextRef myOutContext = NULL;
CFURLRef url;
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle,
false);

if (url != NULL) {
myOutContext = CGPDFContextCreateWithURL (url,
&inMediaBox,
NULL);
CFRelease(url);
}
return myOutContext;
}

关于iphone - 从 UIScrollView + iphone 生成 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5268943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com