我试图将.pdf发送到我的应用,让它将每个页面修改为.png,编辑照片,然后将它们修改为一个.pdf。我可以将.pdf转换为多个.png但尝试将它们转换回一个.pdf给我带来的问题是它将每个页面/图像导出到它自己的pdf而不是一个。这只是快速而肮脏的代码,我试图发送.pdf,转换为.pngs,然后回到.pdf而不编辑.pngs。我查看了http://code.tutsplus.com/tutorials/generating-pdf-documents--mobile-11265和Creating a single page pdf file with array of images in iOS?,但图片不在数组中......任何帮助都会非常感激。
- (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;
}
答案 0 :(得分:0)
您的问题是UIGraphicsBeginPDFContextToFile和CGContextRelease(pdfContext)语句应该在for循环之外。
答案 1 :(得分:0)
试试这个(假设您已将所有图像作为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];