如何从 NSDocumentsDirectory 加载自制的 TIFF 图像文件 [英] How to load a self-made TIFF image file from NSDocumentsDirectory

查看:31
本文介绍了如何从 NSDocumentsDirectory 加载自制的 TIFF 图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从 iPhone 的 NSDocumentsDirectory 加载文件时,我无法使用我拥有的路径字符串创建图像源.

When trying to load files from the NSDocumentsDirectory of an iPhone, I cannot create the image source with the path string I have.

我收到错误消息: ImageIO: CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource failed with error code -15.",这是一个 kCFURLImproperArgumentsError.

I get the error message "<Error>: ImageIO: CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource failed with error code -15.", which is a kCFURLImproperArgumentsError.

但是,我找不到正确"的论点可能是什么.一个示例路径是:/var/mobile/Applications/36D76BDF-72EC-4180-9246-CF1F50CF396C/Documents/2013080110555532Image.tiff"

However, I cannot find what "proper" arguments might be. An example path would be: "/var/mobile/Applications/36D76BDF-72EC-4180-9246-CF1F50CF396C/Documents/2013080110555532Image.tiff"

在我的问题 如何保存 TIFF...,我现在记录如何将文件写入 NSDocumentsDirectory.再次感谢各位的帮助.

In my question How to save a TIFF..., I now document how I write the files to the NSDocumentsDirectory. Thanks again for the help, folks.

现在我尝试再次阅读它们.我认为阅读就像写作,只是镜像.
这些文件大约为 20MB - ...咳咳,...每个!- 这也可能是个问题...

Now I try to read them again. I thought the reading was like the writing, only mirrored.
The files are ~20MB - ...ahem, ...each! - that might be a problem as well...

这是一个代码片段(感谢 Ganapathy 对于 这个答案)实际上给了我一个要显示的图像:

This is a code snippet (thanks to Ganapathy for this answer) that actually gives me an image to display:

if ([self pathLastRawDataSave])
{
    UIImage *anImage = [UIImage imageWithContentsOfFile:[self pathLastRawDataSave]];

    if (NULL != anImage)
    {
        [[self imageView] setImage:anImage];
    }
}

然而,它总是比一个小例子更复杂,我还需要元数据(和图像的缩略图),所以我担心我回来制作 CGImageSource 工作,毕竟.

However, it is always more complicated than in a small example and I also need the metadata (and a thumbnail of the image), so I fear I am back to make the CGImageSource work, after all.

嘿嘿!我找到了!缺少的链接是方法 fileURLWithPath!除此之外,我没有缓存,也没有使用创建选项字典来检索图像.

Heureka! I have found it! The missing link was the method fileURLWithPath! Apart from that I am not caching and also using the create options dictionary for the image retrieval.

这确实有效:

if ([self pathLastRawDataSave])
{
    NSURL* imageFileURL = [NSURL fileURLWithPath:[self pathLastRawDataSave]];

    CFURLRef imageFileURLRef = (__bridge CFURLRef)imageFileURL; // bridged from NS-Object, no release!

    NSDictionary* sourceOptions = @{(id)kCGImageSourceShouldCache: (id)kCFBooleanFalse,
                                    (id)kCGImageSourceTypeIdentifierHint: (id)kUTTypeTIFF};

    CFDictionaryRef sourceOptionsRef = (__bridge CFDictionaryRef)sourceOptions; // bridged from NS-Object, no release!

    CGImageSourceRef imageSource = CGImageSourceCreateWithURL(imageFileURLRef, sourceOptionsRef);


    if (NULL != imageSource)
    {
        // create the image from the file
        CGImageRef imageData = CGImageSourceCreateImageAtIndex(imageSource, 0, sourceOptionsRef);

        // release core foundation object
        CFRelease(imageSource);
    }
    else
    {
        // Error opening image file
        HLSLoggerFatal(@"Image source could not be accessed at path %@", [self pathLastRawDataSave]);
    }
}

推荐答案

Heureka!我找到了!缺少的链接是方法 fileURLWithPath!除此之外,我没有缓存,也没有使用创建选项字典来检索图像.

Heureka! I have found it! The missing link was the method fileURLWithPath! Apart from that I am not caching and also using the create options dictionary for the image retrieval.

这确实有效:

if ([self pathLastRawDataSave])
{
    NSURL* imageFileURL = [NSURL fileURLWithPath:[self pathLastRawDataSave]];

    CFURLRef imageFileURLRef = (__bridge CFURLRef)imageFileURL; // bridged from NS-Object, no release!

    NSDictionary* sourceOptions = @{(id)kCGImageSourceShouldCache: (id)kCFBooleanFalse,
                                    (id)kCGImageSourceTypeIdentifierHint: (id)kUTTypeTIFF};

    CFDictionaryRef sourceOptionsRef = (__bridge CFDictionaryRef)sourceOptions; // bridged from NS-Object, no release!

    CGImageSourceRef imageSource = CGImageSourceCreateWithURL(imageFileURLRef, sourceOptionsRef);


    if (NULL != imageSource)
    {
        // create the image from the file
        CGImageRef imageData = CGImageSourceCreateImageAtIndex(imageSource, 0, sourceOptionsRef);

        // release core foundation object
        CFRelease(imageSource);
    }
    else
    {
        // Error opening image file
        HLSLoggerFatal(@"Image source could not be accessed at path %@", [self pathLastRawDataSave]);
    }
}

这篇关于如何从 NSDocumentsDirectory 加载自制的 TIFF 图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆