0

我正在尝试创建一个由大约 30 页(仅 2mb)组成的 Pdf,但该应用程序过去常常收到内存警告并经常崩溃。我曾经使用 UIGraphicsBeginImageContextWithOptions 来获取一些图像,还使用 ​​renderInContext 来获取 xibs 的图像并写入文件。

示例代码

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);
    }

}

4

0 回答 0