TIFFOpen 在 ios 中总是返回 NULL
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确实需要一个完整路径来访问受到严格限制的 iOS 文件系统上的文件。您只能在应用程序的私有目录中读取和写入文件。每个应用程序在文件系统中都有一个独特的区域。应用程序的目录名称是一长串字母和数字,可以使用 getenv() 进行查询。这是 C 版本:
更新: 由于此方法可能存在一些长期兼容性问题,另一个选择是使用 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:
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.
您需要该文件的完整路径。文件通常写入应用程序的Document目录。
以下是如何获取名为 output.tif 的文件的 Documents 目录的路径,包括获取“c”字符串表示形式:
NSLog(@"cPath %s", cPath);
NSLog 输出:
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:
NSLog(@"cPath %s", cPath);
NSLog output: