Reputation: 363
I have a folder in image.xcassets which has more than 50 images for both iPhone and iPad. I don't want to hard code all the names programatically. Can I get the list of images in that folder in an NSArray?
Upvotes: 1
Views: 1896
Reputation: 1943
I'm not sure if this fully answers your question, but should you normally do this with the method
- (NSArray *)contentsOfDirectoryAtURL:(NSURL *)url includingPropertiesForKeys:(NSArray *)keys options:(NSDirectoryEnumerationOptions)mask error:(NSError **)error
?
This is a code snippet from a program I wrote to get all the images from a directory:
-(void)getContentOfImageDirectory
{
//Emptying the image directory content array
[_imageDirectoryContent removeAllObjects];
//Using NSFileManager to load the content of the image directory in a temporary array
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *tempArray = [fm contentsOfDirectoryAtURL: _imageDirectory includingPropertiesForKeys: _imageProperties options: NSDirectoryEnumerationSkipsPackageDescendants error: nil];
//Copy the temporary array into the imageDirectoryContent before returning the NSMutableArray imageDirectoryContent
[_imageDirectoryContent addObjectsFromArray:tempArray];
}
The variable _imageProperties
is just an array of "common file system resource keys" as Apple calls them. The variable _imageDirectory
is the URL from which you want to get the files.
hope this helps.
Upvotes: 1
Reputation: 1943
I'm sorry to annoy you and misunderstood your question. However, if I use the URL file///User/<Your Userid>/your file path to the program/Images.xcassets/
I get the content of that directory.
On the other hand if I use URLsForDirectory:NSApplicationDirectory inDomains:NSUserDomainMask
and then
URLByAppendingPathComponent:@"Your Application container/Contents/Resources"
I can read all the image files of any fully compiled and operational application. I'm not aware of determining the application's resource folder in any other way.
This is a code snippet for accessing the resources directory of the Windows 7 applications folder from parallels.
-(id)initWithStartURL
{
self = [super init];
if(self)
{
//Initiating the file manager
NSFileManager *fm = [NSFileManager defaultManager];
//Getting the applications directory
NSArray *listOfURLs = [fm URLsForDirectory:NSApplicationDirectory inDomains:NSUserDomainMask];
if([listOfURLs count] >= 1)
{
_tempDirectory = [listOfURLs objectAtIndex:0];
}
_imageDirectory = [_tempDirectory URLByAppendingPathComponent:@"Windows 7 Applications.app/Contents/Resources"];
}
return self;
}
Upvotes: 0