2015-05-09 124 views
1

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

回答

0

你的問題是,UIGraphicsBeginPDFContextToFile和CGContextRelease(pdfContext)語句應該在你的for循環之外。

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];