I'm trying to send a .pdf to my app, have it convent each page to a .png, edit photo, then convent them back into one .pdf. I can convert the .pdf to multiple .pngs but trying to convert them back into one .pdf is giving me problems it is exporting each page/image into its own pdf not one. This is just the quick and dirty code where i'm trying to send a .pdf, convert to .pngs, then back to a .pdf with out the editing of the .pngs right now. I've looked at http://code.tutsplus.com/tutorials/generating-pdf-documents--mobile-11265 and Creating a single page pdf file with array of images in iOS? but the images are not in an array... any help would be greatly appreciated.
我正在尝试将.pdf发送到我的应用程序,将每个页面修改为.png,编辑照片,然后将它们修改为一个.pdf。我可以将.pdf转换为多个.png但尝试将它们转换回一个.pdf给我带来的问题是它将每个页面/图像导出到它自己的pdf而不是一个。这只是快速而肮脏的代码,我正在尝试发送.pdf,转换为.pngs,然后回到.pdf而不编辑.pngs。我查看了http://code.tutsplus.com/tutorials/generating-pdf-documents--mobile-11265和在iOS中创建包含图像数组的单页pdf文件?但图像不在数组中...任何帮助将不胜感激。
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
NSLog(@"Open URL:\t%@\n"
"From source:\t%@\n"
"With annotation:%@",
url, sourceApplication, annotation);
CGPDFDocumentRef SourcePDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);
size_t numberOfPages = CGPDFDocumentGetNumberOfPages(SourcePDFDocument);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathAndDirectory = [documentsDirectory stringByAppendingPathComponent:@"temp"];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(@"Create directory error: %@", error);
[[NSFileManager defaultManager] removeItemAtPath:filePathAndDirectory error:&error];
NSLog(@"other error: %@", error);
[[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error];
}
NSMutableData *pdfFile = [[NSMutableData alloc] init];
CGDataConsumerRef pdfConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfFile);
for(int currentPage = 1; currentPage <= numberOfPages; currentPage ++ )
{
CGPDFPageRef SourcePDFPage = CGPDFDocumentGetPage(SourcePDFDocument, currentPage);
// CoreGraphics: MUST retain the Page-Refernce manually
CGPDFPageRetain(SourcePDFPage);
NSString *relativeOutputFilePath = [NSString stringWithFormat:@"%@/%@%d.png", @"temp", @"photo", currentPage];
NSString *ImageFileName = [documentsDirectory stringByAppendingPathComponent:relativeOutputFilePath];
CGRect sourceRect = CGPDFPageGetBoxRect(SourcePDFPage, kCGPDFCropBox);
UIGraphicsBeginPDFContextToFile(ImageFileName, sourceRect, nil);
UIGraphicsBeginImageContextWithOptions(sourceRect.size, NO, 3);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0.0, sourceRect.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawPDFPage (currentContext, SourcePDFPage); // draws the page in the graphics context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent: relativeOutputFilePath];
CGDataConsumerRef pdfConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfFile);
// The page size matches the image, no white borders.
CGRect mediaBox = CGRectMake(0, 0, sourceRect.size.width, sourceRect.size.height);
CGContextRef pdfContext = CGPDFContextCreate(pdfConsumer, &mediaBox, NULL);
CGContextBeginPage(pdfContext, &mediaBox);
CGContextDrawImage(pdfContext, mediaBox, [image CGImage]);
CGContextEndPage(pdfContext);
CGContextRelease(pdfContext);
CGDataConsumerRelease(pdfConsumer);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *end = @".pdf";
NSString *start = @"Documents/image";
NSString * timestamp = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"%@%@%@", start, timestamp, end]];
[pdfFile writeToFile:path atomically:YES];
NSLog([NSString stringWithFormat:@"%@%@%@", start, timestamp, end]);
}
return YES;
}
2 个解决方案
#1
Your problem is that UIGraphicsBeginPDFContextToFile and CGContextRelease(pdfContext) statements should be outside your for loop.
您的问题是UIGraphicsBeginPDFContextToFile和CGContextRelease(pdfContext)语句应该在for循环之外。
#2
Try this (assuming you've placed all images as a UIImageView on a UIScrollView):
试试这个(假设您已将所有图像作为UIImageView放在UIScrollView上):
/* CREATE PDF */
self.pdfData = [NSMutableData data];
NSInteger pageHeight = 800;
UIGraphicsBeginPDFContextToData(self.pdfData, CGRectMake(0,0,768,pageHeight), nil);//768*792/612
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
for (int page=0; pageHeight *page < scrollView.frame.size.height; page++)
{
UIGraphicsBeginPDFPage();
CGContextTranslateCTM(pdfContext, 0, -pageHeight * page);
[scrollView.layer renderInContext:pdfContext];
}
UIGraphicsEndPDFContext();
/* EXPORT PDF */
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* fileName = [self genRandStringLength:7];
fileName = [NSString stringWithFormat:@"%@.pdf", fileName];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:fileName];
// instructs the mutable data object to write its context to a file on disk
[self.pdfData writeToFile:documentDirectoryFilename atomically:YES];
#1
Your problem is that UIGraphicsBeginPDFContextToFile and CGContextRelease(pdfContext) statements should be outside your for loop.
您的问题是UIGraphicsBeginPDFContextToFile和CGContextRelease(pdfContext)语句应该在for循环之外。
#2
Try this (assuming you've placed all images as a UIImageView on a UIScrollView):
试试这个(假设您已将所有图像作为UIImageView放在UIScrollView上):
/* CREATE PDF */
self.pdfData = [NSMutableData data];
NSInteger pageHeight = 800;
UIGraphicsBeginPDFContextToData(self.pdfData, CGRectMake(0,0,768,pageHeight), nil);//768*792/612
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
for (int page=0; pageHeight *page < scrollView.frame.size.height; page++)
{
UIGraphicsBeginPDFPage();
CGContextTranslateCTM(pdfContext, 0, -pageHeight * page);
[scrollView.layer renderInContext:pdfContext];
}
UIGraphicsEndPDFContext();
/* EXPORT PDF */
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* fileName = [self genRandStringLength:7];
fileName = [NSString stringWithFormat:@"%@.pdf", fileName];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:fileName];
// instructs the mutable data object to write its context to a file on disk
[self.pdfData writeToFile:documentDirectoryFilename atomically:YES];