2012-02-13 91 views
0

我想用Libtiff創建TIFF圖像。我無法弄清楚文件無法打開的原因。任何人有任何想法?TIFFOpen總是在ios中返回NULL

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

任何幫助將不勝感激。 謝謝

+0

我想用這段代碼來寫tiff文件。這是一個c代碼。 – pa12 2012-02-14 22:45:46

+0

這是我嘗試在ios中使用的c函數。 – pa12 2012-02-15 14:25:06

+0

至少把代碼放在自動釋放池和'#import '中,這樣你就可以使用iOS方法,比如'NSSearchPathForDirectoriesInDomains'。 – zaph 2012-02-15 14:51:19

回答

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目錄。

+0

你能給出一個使用NSSearchPathForDirectoriesInDomains僅使用C代碼來查找名稱的完整示例嗎? – BitBank 2012-02-14 22:25:47

+0

說這不是iOS是不正確的。在iPhone上編寫純粹的C應用程序當然是可能的。你最終將得到一個沒有GUI功能的unix命令行應用程序。我經常使用這種技術在iPhone/iPad上編寫測試應用程序。 – BitBank 2012-02-14 23:30:31

+0

獲取iOS中Documents目錄(或任何其他可寫目錄)路徑的首選方法是使用'NSSearchPathForDirectoriesInDomains'。除了Documents目錄外,還有其他可寫目錄。 – zaph 2012-02-15 14:59:01

1

您需要一個文件的完整路徑。文件通常寫入應用程序的文檔目錄。

這裏是如何得到的路徑,文件目錄名爲output.tif包括得到一個 「C」 字符串表示的文件:

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 
+0

這是在tiffLib中使用的c代碼。 TIFFOpen不會將filepath作爲參數。 – pa12 2012-02-13 17:01:00

+0

明白了你的觀點。但是這是使用libtiff創建一個新的tiff文檔時給出的代碼。 – pa12 2012-02-14 14:38:23