【问题标题】:Converting multiple UIImages stored in NSMutableArray to PDF将存储在 NSMutableArray 中的多个 UIImage 转换为 PDF
【发布时间】:2025-07-11 15:25:03
【问题描述】:

我正在将 PDF 文件的每一页转换为 UIImage 并将其存储在 NSMutableArray 中。现在我想像以前一样将相同的 UIImages 转换为 PDF 文件。我可以转换单个图像,但我希望一次转换所有图像,并且应该与以前的 PDF 文件相同。

我将 UIImage 转换为 PDF 页面的代码:

UIImage *image1 = [ UIImage originalSizeImageWithPDFNamed:[NSString stringWithFormat:@"%@%@",fileName,@".pdf"] atPage:i+1 ];
imageData = [NSData dataWithData:UIImagePNGRepresentation(image1)];
[thumbImage addObject:[UIImage imageWithData:imageData]];

如何将整个 UIImages 转换为单个 PDF 文件?

【问题讨论】:

    标签: ios objective-c pdf uiimage nsmutablearray


    【解决方案1】:

    使用这个方法:

    - (void)createPDFWithImagesArray:(NSMutableArray *)array andFileName:(NSString *)fileName
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *PDFPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",fileName]];
    
        UIGraphicsBeginPDFContextToFile(PDFPath, CGRectZero, nil);
        for (UIImage *image in array)
        {
            // Mark the beginning of a new page.
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, image.size.width, image.size.height), nil);
    
            [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
        }
        UIGraphicsEndPDFContext();
    }
    

    【讨论】:

    • 我用我的 iPhone 写了这个方法,所以我希望没有任何语法错误。这应该可以工作(;
    最近更新 更多