How to load a self-made TIFF image file from NSDocumentsDirectory

六眼飞鱼酱① 提交于 2019-12-12 05:59:23

问题


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

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

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"

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.

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...

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

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.

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.

This is actually working:

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

回答1:


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.

This is actually working:

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


来源:https://stackoverflow.com/questions/17673587/how-to-load-a-self-made-tiff-image-file-from-nsdocumentsdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!