如何在目标c中创建具有内存效率的PDF(大)?

时间:2013-04-12 06:00:49

标签: ios objective-c

我正在尝试创建一个页面大约30(仅2mb)的Pdf,但该应用程序用于接收内存警告并经常崩溃。 我以前使用UIGraphicsBeginImageContextWithOptions来获取一些图像,还使用renderInContext来获取xib的图像并写入文件。

示例代码

UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
                        [view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
                        imageView.image = nil;
                        imageView = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
                        UIGraphicsEndImageContext();
                        NSString *name = [NSString stringWithFormat:@"Screen_%d%@",item,imageNumber];
                        NSString  *pngPath = PDFTempResourceDirectory();
                        pngPath = [pngPath stringByAppendingPathComponent:name];
                        [UIImagePNGRepresentation(imageView.image) writeToFile:pngPath atomically:YES];

以下是我用于创建pdf的代码

 -(void)setPDFOutPutPath:(NSString*)path{

NSString *pdfPathOutput = [PDFFileDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",path]];
CFURLRef pdfURLOutput = (__bridge CFURLRef)[NSURL fileURLWithPath:pdfPathOutput isDirectory:NO];
writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);
CGContextSetInterpolationQuality(writeContext,kCGInterpolationNone);
pdfPathOutput = nil;
}


-(void)appendPDfFile:(NSString *)path{

CFURLRef pdfURL;
CGPDFDocumentRef pdfRef;
NSInteger numberOfPages;
CGPDFPageRef page;
CGRect mediaBox;

pdfURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path isDirectory:NO];
pdfRef = CGPDFDocumentCreateWithURL(pdfURL);
numberOfPages = CGPDFDocumentGetNumberOfPages(pdfRef);

for (int i=1; i<=numberOfPages; i++) {
    page = CGPDFDocumentGetPage(pdfRef, i);
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, page);
    CGContextEndPage(writeContext);
}
CGPDFDocumentRelease(pdfRef);
}

用于创建pdf

///

(void)createSinglePDF:(CALayer *)inputView with:(int)pageNumber{

    @autoreleasepool {

        NSMutableData *pdfData = [NSMutableData data];
        UIGraphicsBeginPDFContextToData(pdfData, firstPage.bounds, nil);
        CGContextRef pdfContext = UIGraphicsGetCurrentContext();
        UIGraphicsBeginPDFPage();
        [inputView renderInContext:pdfContext];
        UIGraphicsEndPDFContext();
//        if(pdfContext)
//            CGContextClearRect(pdfContext, firstPage.bounds);
//        if(pdfContext)
//            CGContextFlush(pdfContext);
        NSString *fileName = [NSString stringWithFormat:@"PDFReport_%d.pdf",pageNumber];
        NSString *documentDirectoryFilename = [PDFFileDirectory() stringByAppendingPathComponent:fileName];
        BOOL isSaved =[pdfData writeToFile:documentDirectoryFilename atomically:YES];
        NSString *pdfPath  = [PDFFileDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"PDFReport_%d.pdf",pageNumber]];
        [self appendPDfFile:pdfPath];
            // CGPDFContextClose(pdfContext);
       // CGContextRelease(pdfContext);
    }

}

0 个答案:

没有答案