Problem with [NSImage imageFileTypes] in two different contexts
Problem with [NSImage imageFileTypes] in two different contexts
- Subject: Problem with [NSImage imageFileTypes] in two different contexts
- From: Michael Peirce <email@hidden>
- Date: Fri, 29 Mar 2002 23:30:51 -0500
I've run into a little problem and wanted to solicit work around ideas.
I seem to have run into a case where the slightly confused story on
using file extensions vs. old style file types leads to a problem.
In my app I let users select graphics files. I use the
runModalForDirectory method for this using the types parameter to select
only graphics files as returned by [NSImage imageFileTypes].
This works fine. It lets the user select files like "MyPicture.jpg" and
even "MyOldPicture" if it is of type 'jpeg' for example.
Later I scan through the folder the file was in looking for all the
graphics files.
I enumerate all the files in the folder and then use
filesOfImageType = [filenames
pathsMatchingExtensions:imageFileTypes];
This passes in the exact same imageFileTypes, but in this case it only
considers the actual files with "proper" extensions. It obviously can't
consider old style file types since it really doesn't know which files
we're talking about, it only sees the filename.
I suppose I can skip using pathsMatchingExtensions and instead do the
matching myself as the files are being enumerated. I'd have to check
extensions for a match as well as ask for the file's type and match
against that.
That's not really so bad, but I was wondering if anyone has run up
against this already and/or has any cleaver ideas on how to do this.
I'm fixing this in my recently release DeskPicture 10. It turns out that
in the old system folder there are desktop pictures whose filenames do
no have extensions, they are files of type 'jpeg'. A user selected one
of these, turned on my "change the picture" option and DeskPicture 10
tries to select a new picture from this folder. It can't find any valid
NSImage compatible files there. In DeskPicture 10's case, when it can't
find any file in the folder it defaults back to the original file which
can be read just fine and Deskpicture 10 displays it, but nothing ever
changes like the user requested.
To see these files look in:
/Volumes/MyOldDisk/System Folder/Appearance/Desktop Pictures/3D
Graphics/
-- michael peirce (fixing reported bug #5 in DeskPicture 10)
FWIW, below is some test code that shows this issue - assume a little
UI of an obvious nature :-)
- (IBAction)displayFilesInFolder:(id)sender
{
NSString *filename;
NSArray *filesOfImageType;
NSArray *imageFileTypes = [NSImage imageFileTypes];
NSString *thePath = [pathText stringValue];
NSMutableArray *filenames = [NSMutableArray arrayWithCapacity:100];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]
enumeratorAtPath: thePath];
while (filename = [enumerator nextObject]) {
[filenames addObject:filename];
}
filesOfImageType = [filenames
pathsMatchingExtensions:imageFileTypes];
[filteredFiles setString: [filesOfImageType
componentsJoinedByString:@"\r"]];
[allFiles setString: [filenames componentsJoinedByString:@"\r"]];
}
- (IBAction)displayDialog:(id)sender
{
NSArray *imageFileTypeArray = [NSImage imageFileTypes];
NSString *defaultFile = nil;
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
NSString *defaultDir = [pathText stringValue];
[openPanel runModalForDirectory:defaultDir file:defaultFile
types:imageFileTypeArray];
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.