从 image.xcassets 中的文件夹中获取图像列表



>我在image.xcasset中有一个文件夹,里面有50多张iPhone和iPad的图像。我不想以编程方式对所有名称进行硬编码。我可以在 NSArray 中获取该文件夹中的映像列表吗?

我不确定这是否完全回答了您的问题,但是您通常应该使用该方法执行此操作

- (NSArray *)contentsOfDirectoryAtURL:(NSURL *)url includingPropertiesForKeys:(NSArray *)keys options:(NSDirectoryEnumerationOptions)mask error:(NSError **)error

这是我编写的程序的代码片段,用于从目录中获取所有图像:

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

变量_imageProperties只是Apple所说的"公共文件系统资源键"数组。变量 _imageDirectory 是要从中获取文件的 URL。希望这有帮助。

抱歉惹恼了你,误解了你的问题。但是,如果我使用 URL file///User/<Your Userid>/your file path to the program/Images.xcassets/我会得到该目录的内容。

另一方面,如果我使用URLsForDirectory:NSApplicationDirectory inDomains:NSUserDomainMask 然后

URLByAppendingPathComponent:@"Your Application container/Contents/Resources" 

我可以读取任何完全编译和操作的应用程序的所有图像文件。我不知道以任何其他方式确定应用程序的资源文件夹。

这是用于从并行访问 Windows 7 应用程序文件夹的资源目录的代码片段。

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

最新更新