简体   繁体   English

TIFFOpen总是在ios中返回NULL

[英]TIFFOpen always return NULL in ios

I am trying to create TIFF image using Libtiff. 我正在尝试使用Libtiff创建TIFF图像。 I could not figure out the reason why the file is unable to be opened. 我无法弄清楚无法打开文件的原因。 Anyone have any idea?? 任何人都有任何想法?

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

Edit 1 编辑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);
}

Any help will be appreciated. 任何帮助将不胜感激。 Thanks 谢谢

You need a full path to the file. 您需要文件的完整路径。 Files are usually written to the applications Document directory. 文件通常写入应用程序文档目录。

Here is how to get the path to the Documents directory for a file named output.tif including getting a "c" string representation: 以下是如何为名为output.tif的文件获取Documents目录的路径,包括获取“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%s”,cPath); NSLog output: NSLog输出:

cPath /Volumes/User/dgrassi/Library/Application Support/iPhone Simulator/5.0/Applications/D483A43F-E8DD-4C80-81CF-E2F0CDF3EF49/Documents/output.tif

You DO need a full path to access files on the wonderfully restricted iOS file system. 您需要一个完整的路径来访问受限制的iOS文件系统上的文件。 You can only read and write files into your app's private directories. 您只能读取和写入应用程序私人目录中的文件。 Each app has a unique area in the filesystem. 每个应用程序在文件系统中都有一个唯一的区域。 The app's directory name is a long series of letters and numbers that can be queried with getenv(). 应用程序的目录名称是一长串字母和数字,可以使用getenv()进行查询。 Here's the C version: 这是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");
   }

Update: Since there may be some long term compatibility issues with this method, another option is to use argv[0] (the full path to your executable) and trim off the leaf name and modify it to point to the Documents directory. 更新:由于此方法可能存在一些长期兼容性问题,另一个选项是使用argv [0](可执行文件的完整路径)并修剪叶名称并将其修改为指向Documents目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM