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