TIFFOpen 在 ios 中总是返回 NULL

发布于 2025-01-05 21:35:24 字数 1454 浏览 4 评论 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);
}

任何帮助将不胜感激。 谢谢

I am trying to create TIFF image using Libtiff. 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

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離人涙 2025-01-12 21:35:25

您确实需要一个完整路径来访问受到严格限制的 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 目录。

You DO need a full path to access files on the wonderfully restricted iOS file system. 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(). Here's the C version:

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.

眼眸里的那抹悲凉 2025-01-12 21:35:25

您需要该文件的完整路径。文件通常写入应用程序的Document目录。

以下是如何获取名为 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 /Volumes/User/dgrassi/Library/Application Support/iPhone Simulator/5.0/Applications/D483A43F-E8DD-4C80-81CF-E2F0CDF3EF49/Documents/output.tif

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:

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 output:

cPath /Volumes/User/dgrassi/Library/Application Support/iPhone Simulator/5.0/Applications/D483A43F-E8DD-4C80-81CF-E2F0CDF3EF49/Documents/output.tif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文