我試圖發送一個.pdf到我的應用程序,讓它修改每個頁面到一個.png,編輯照片,然後修改它們回到一個.pdf。我可以將.pdf轉換爲多個.png,但試圖將它們轉換回.pdf,它會給我帶來問題,它會將每個頁面/圖像導出到它自己的pdf中,而不是一個。這只是我試圖發送.pdf的快速和骯髒的代碼,轉換爲.pngs,然後返回到.pdf,現在不用編輯.png。我已經看過http://code.tutsplus.com/tutorials/generating-pdf-documents--mobile-11265和Creating a single page pdf file with array of images in iOS?,但圖像不在數組中...任何幫助將不勝感激。將多個.png結合成一個.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;
}