0

画像を PDF 形式でドキュメント ディレクトリに保存します。保存形式を教えてください。iOS での保存形式は PNG と JPEG しか知りません。

4

2 に答える 2

3

まず、PDF ページのサイズを計算します。画像サイズと画像を描画する解像度に基づいて、ページ サイズは次のように計算されます (柔軟性のために、さまざまな水平解像度と垂直解像度がサポートされています)。

 double pageWidth = image.size.width * image.scale * 72 / horzRes;
 double pageHeight = image.size.height * image.scale * 72 / vertRes;

これで、PDF ファイルを作成する準備が整いました。ディスク上またはメモリ内に直接作成できます。柔軟性のために、メモリ内に作成しましょう。

NSMutableData *pdfFile = [[NSMutableData alloc] init];
CGDataConsumerRef pdfConsumer = 
    CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfFile);
// The page size matches the image, no white borders.
CGRect mediaBox = CGRectMake(0, 0, pageWidth, pageHeight);
CGContextRef pdfContext = CGPDFContextCreate(pdfConsumer, &mediaBox, NULL);

PDF コンテキストが作成され、変換を実行できるようになりました。

CGContextBeginPage(pdfContext, &mediaBox);
CGContextDrawImage(pdfContext, mediaBox, [image CGImage]);
CGContextEndPage(pdfContext);

作業が完了し、PDF ファイルをファイナライズして、使用したオブジェクトを解放します。

CGContextRelease(pdfContext); CGDataConsumerRelease(pdfConsumer);

PDF ファイルが pdfFile 変数で使用できるようになりました。PDFImageConverter ユーティリティ クラスの convertImageToPDF:withHorizo​​ntalResolution:verticalResolution: static メソッドに上記のすべてのコードを配置しました。次のように使用できます。

NSData *pdfData = [PDFImageConverter convertImageToPDF: image 
    withHorizontalResolution: 300 verticalResolution: 300];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];

場合によっては、画像を PDF に変換し、レターや A4 などの定義済みのページ サイズを使用する必要があります。また、画像の最大外接矩形を指定できるため、ページ上の特定の位置に配置され、ページからはみ出ることはありません。この状況では、画像が所定の長方形に収まるように、画像サイズを調整 (解像度を上げる) する必要があります。

double imageWidth = image.size.width * image.scale * 72 / resolution;
double imageHeight = image.size.height * image.scale * 72 / resolution;

double sx = imageWidth / boundsRect.size.width;
double sy = imageHeight / boundsRect.size.height;

// At least one image edge is larger than maxBoundsRect, reduce its size.
if ((sx > 1) || (sy > 1)) {
    double maxScale = sx > sy ? sx : sy;
    imageWidth = imageWidth / maxScale;
    imageHeight = imageHeight / maxScale;
}


// Put the image in the top left corner of the bounding rectangle
CGRect imageBox = CGRectMake(
    boundsRect.origin.x, boundsRect.origin.y + boundsRect.size.height - imageHeight, 
    imageWidth, imageHeight);

最後のボックスができたら、記事の前半のコードを使用して画像を PDF に変換できます。新しい変換メソッドのコードは、PDFImageConverter ユーティリティ クラスの convertImageToPDF:withResolution:maxBoundsRect:pageSize: 静的メソッドに配置され、次のように使用されます。

CGSize pageSize = CGSizeMake(612, 792);
CGRect imageBoundsRect = CGRectMake(50, 50, 512, 692);

NSData *pdfData = [PDFImageConverter convertImageToPDF: image 
    withResolution: 300 maxBoundsRect: imageBoundsRect pageSize: pageSize];

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];

また、ソース コードフォームをダウンロードすることもできます。

于 2013-05-28T06:43:40.650 に答える
0

このチュートリアルconvert-an-image-to-pdf-on-the-iphone-and-ipadを使用できます

次に、次を使用してpdfファイルをドキュメントディレクトリに保存します。

//******** Method To Copy Image to Directory ******** //

- (void) copyFileToDocuments:(NSString *)fileURL
{
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/convertedImage.pdf"];
    NSError *error;
    [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:fileURL] toURL:[NSURL fileURLWithPath:destinationPath] error:&error];

}

それから電話する

    [self  copyFileToDocuments:moviePath];

お役に立てば幸いです。

于 2013-05-28T06:44:50.853 に答える