// documents dirNSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES);Nsstring *documentsDirectory = [paths objectAtIndex:0];// file pathsNsstring *pdfPath1 = [documentsDirectory stringByAppendingPathComponent:@"1.pdf"];Nsstring *pdfPath2 = [documentsDirectory stringByAppendingPathComponent:@"2.pdf"];Nsstring *pdfPathOutput = [documentsDirectory stringByAppendingPathComponent:@"out.pdf"];// file URLsCFURLRef pdfURL1 = (CFURLRef)[[NSURL alloc] initfileURLWithPath:pdfPath1];CFURLRef pdfURL2 = (CFURLRef)[[NSURL alloc] initfileURLWithPath:pdfPath2];CFURLRef pdfURLOutput = (CFURLRef)[[NSURL alloc] initfileURLWithPath:pdfPathOutput];// file referencesCGpdfdocumentRef pdfRef1 = CGpdfdocumentCreateWithURL((CFURLRef) pdfURL1);CGpdfdocumentRef pdfRef2 = CGpdfdocumentCreateWithURL((CFURLRef) pdfURL2);// Number of pagesNSInteger numberOfPages1 = CGpdfdocumentGetNumberOfPages(pdfRef1);NSInteger numberOfPages2 = CGpdfdocumentGetNumberOfPages(pdfRef2);// Create the output contextCGContextRef writeContext = CGPdfcontextCreateWithURL(pdfURLOutput,NulL,NulL);// Loop variablesCGpdfpageRef page;CGRect mediaBox;// Read the first pdf and generate the output pagesNSLog(@"GENErating PAGES FROM pdf 1 (%i)...",numberOfPages1);for (int i=1; i<=numberOfPages1; i++) { page = CGpdfdocumentGetPage(pdfRef1,i); mediaBox = CGpdfpageGetBoxRect(page,kCGpdfMediaBox); CGContextBeginPage(writeContext,&mediaBox); CGContextDrawpdfpage(writeContext,page); CGContextEndPage(writeContext);}// Read the second pdf and generate the output pagesNSLog(@"GENErating PAGES FROM pdf 2 (%i)...",numberOfPages2);for (int i=1; i<=numberOfPages2; i++) { page = CGpdfdocumentGetPage(pdfRef2,page); CGContextEndPage(writeContext); }NSLog(@"DONE!");// Finalize the output fileCGPdfcontextClose(writeContext);// Release from memoryCFRelease(pdfURL1);CFRelease(pdfURL2);CFRelease(pdfURLOutput);CGpdfdocumentRelease(pdfRef1);CGpdfdocumentRelease(pdfRef2);CGContextRelease(writeContext);
这里最大的问题是内存分配.您可以看到,在这种方法中,您必须阅读要合并的两个pdf文件,同时生成输出.这些版本只在最后发生.我尝试将一个500页(约15MB)的pdf文件与另外一个包含100页(〜3MB)的pdf文件结合起来,并且生成了一个只有大约5MB大小(大约为5MB)的600页(当然!)的新的pdf文件.执行时间约为30秒(不用考虑iPad 1),并分配17MB(uch!).该应用程序幸运地没有崩溃,但我认为iOS会喜欢杀死一个这样的消费17MB的应用程序.,P
总结以上是内存溢出为你收集整理的iphone – 在iOS上合并PDF文件全部内容,希望文章能够帮你解决iphone – 在iOS上合并PDF文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)