我的项目中有一个包含图片的文件夹,我想知道如何将这些图片从文件夹中放入数组中
我该怎么做?
我试过这个把图像放在数组中
UIImage*image = [[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" @"jpeg" @"gif" inDirectory:@"Images"];
NSArray*images = [[NSMutableArray alloc]initWithContentsOfFile:image];
我的项目中有一个包含图片的文件夹,我想知道如何将这些图片从文件夹中放入数组中
我该怎么做?
我试过这个把图像放在数组中
UIImage*image = [[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" @"jpeg" @"gif" inDirectory:@"Images"];
NSArray*images = [[NSMutableArray alloc]initWithContentsOfFile:image];
您可以执行以下操作,获取文件名数组,然后用图像本身填充另一个数组(假设这是您想要做的)。
NSMutableArray *imagePaths = [[NSMutableArray alloc] init];
NSMutableArray *images = [[NSMutableArray alloc] init];
NSArray *imageTypes = [NSArray arrayWithObjects:@"jpg", @"jpeg", @"gif", nil];
// load the imagePaths array
for (NSString *imageType in imageTypes)
{
NSArray *imagesOfParticularType = [[NSBundle mainBundle]pathsForResourcesOfType:imageType
inDirectory:@"Images"];
if (imagesOfParticularType)
[imagePaths addObjectsFromArray:imagesOfParticularType];
}
// load the images array
for (NSString *imagePath in imagePaths)
[images addObject:[UIImage imageWithContentsOfFile:imagePath]];
顺便说一句,除非这些是很小的缩略图图像并且您只有很少的图像,否则通常不建议像这样一次加载所有图像。通常,由于图像会占用大量 RAM,因此您会保留文件名数组,但将图像的加载推迟到您真正需要它们时。
如果您没有成功检索图像,则有两个问题:
这些文件包含在我的捆绑包中吗?当您为 Target 的设置选择“Build Phases”并展开“Copy Bundle Resources”(见下图)时,您看到包含的图像了吗?如果您没有在此列表中看到您的图像,则在您构建应用程序时它们不会包含在应用程序中。要添加您的图像,请单击“+”并将它们添加到此列表中。
这些文件是在“组”中还是在实际子目录中?将文件添加到项目时,您将看到以下对话框:
如果您选择“为添加的文件夹创建文件夹引用”,那么该文件夹将在您的项目中显示为蓝色(请参阅db_images
前面屏幕快照中我的“”文件夹旁边的蓝色图标)。但是,如果您选择“为添加的文件夹创建组”,则“ Images
”组旁边会出现典型的黄色图标。最重要的是,在这种情况下,您要在子目录“ Images
”中查找图像,您希望使用“为添加的文件夹创建文件夹引用”选项,并在图像旁边显示蓝色图标。
最重要的是,您需要确保图像在您的应用程序包中,并且它们在您认为的位置。另请注意,iOS 区分大小写(尽管模拟器不区分大小写),因此请确保“ Images
”的大小写正确。
如果我正确理解了您的问题 initWithContentsOfFile 不符合您的预期,根据文档:
“使用给定路径指定的文件内容初始化新分配的数组。”
此外,pathsForResourceOfType 已经在创建一个数组,而不是 UIImage,您可以简单地执行以下操作:
NSArray* images = [[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" @"jpeg" @"gif" inDirectory:@"Images"];
[[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" @"jpeg" @"gif" inDirectory:@"Images"];
已经返回了这些对象的数组。将您的行更改为:
NSArray *images = [[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" @"jpeg" @"gif" inDirectory:@"Images"];
请注意,此数组将仅保存所有图像的路径。为了制作它们的图像,您需要调用
for(NSString* imagePath in images) {
UIImage* anImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
//do something with your image here.
}
希望有帮助
阅读initWithContentsOfFile
方法的文档NSArray
:
由 aPath 标识的文件中的数组表示必须仅包含属性列表对象(NSString、NSData、NSArray 或 NSDictionary 对象)。该数组包含的对象是不可变的,即使该数组是可变的。
在您的情况下,您需要使用NSFileManager
枚举目录中的文件。以下是文档中目录枚举的示例:
NSFileManager *localFileManager=[[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum =
[localFileManager enumeratorAtPath:docsDir];
NSString *file;
while (file = [dirEnum nextObject]) {
if ([[file pathExtension] isEqualToString: @"doc"]) {
// Create an image object here and add it to
// mutable array
}
}
[localFileManager release];
尝试这个:
NSMutableArray* paths=[NSMutableArray new];
NSFileManager* manager=[NSFileManager new];
NSBundle* bundle= [NSBundle mainBundle];
NSDirectoryEnumerator* enumerator= [manager enumeratorAtPath: [bundle bundlePath] ];
for(NSString* path in enumerator)
{
if([path hasSuffix: @".jpg"] || [path hasSuffix: @".jpeg"] || [path hasSuffix: @".gif"])
{
[paths addObject: path];
}
}
对于解释,我建议您查看NSDirectoryEnumerator 文档。