0

Libtiff を使用して TIFF イメージを作成しようとしています。ファイルを開けない理由がわかりませんでした。誰でも何か考えがありますか??

TIFF *image;
// Open the TIFF file
if((image = TIFFOpen("output.tif", "w")) == NULL){
    printf("Could not open output.tif for writing\n");
}

編集 1

#include <stdio.h>
#include <tiffio.h>

int main(int argc, char *argv[]){
// Define an image
char buffer[25 * 144] = { /* boring hex omitted */ };
TIFF *image;

// Open the TIFF file
if((image = TIFFOpen("output.tif", "w")) == NULL){
  printf("Could not open output.tif for writing\n");
exit(42);
}

// We need to set some values for basic tags before we can add any data
TIFFSetField(image, TIFFTAG_IMAGEWIDTH, 25 * 8);
TIFFSetField(image, TIFFTAG_IMAGELENGTH, 144);
TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, 1);
TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, 144);

TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
TIFFSetField(image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

TIFFSetField(image, TIFFTAG_XRESOLUTION, 150.0);
TIFFSetField(image, TIFFTAG_YRESOLUTION, 150.0);
TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);

// Write the information to the file
TIFFWriteEncodedStrip(image, 0, buffer, 25 * 144);

// Close the file
TIFFClose(image);
}

どんな助けでも大歓迎です。ありがとう

4

2 に答える 2

1

ファイルへのフル パスが必要です。ファイルは通常、アプリケーションの Document ディレクトリに書き込まれます。

「c」文字列表現の取得を含む、output.tif という名前のファイルの Documents ディレクトリへのパスを取得する方法を次に示します。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"output.tif"];
const char* cPath = [filePath cStringUsingEncoding:NSMacOSRomanStringEncoding];

NSLog(@"cPath %s", cPath); NSLog 出力:

cPath /Volumes/User/dgrassi/Library/Application Support/iPhone Simulator/5.0/Applications/D483A43F-E8DD-4C80-81CF-E2F0CDF3EF49/Documents/output.tif
于 2012-02-13T15:15:11.250 に答える
1

素晴らしく制限された iOS ファイル システム上のファイルにアクセスするには、絶対パスが必要です。アプリのプライベート ディレクトリに対してのみ、ファイルの読み取りと書き込みを行うことができます。各アプリには、ファイル システム内に固有の領域があります。アプリのディレクトリ名は、getenv() でクエリできる長い一連の文字と数字です。Cバージョンは次のとおりです。

TIFF *image;
char szFileName[512];

   strcpy(szFileName, getenv("HOME"));
   strcat(szFileName, "/Documents/");
   strcat(szFileName, "output.tif");
   // Open the TIFF file
   if((image = TIFFOpen(szFileName, "w")) == NULL)
   {
      printf("Could not open output.tif for writing\n");
   }

更新:この方法には長期的な互換性の問題がある可能性があるため、argv[0] (実行可能ファイルへのフル パス) を使用し、リーフ名を削除して Documents ディレクトリを指すように変更するという別のオプションがあります。

于 2012-02-14T20:14:15.590 に答える