如何从NSDocumentsDirectory
检索所有图片?我不知道图片名称,但我需要将所有图片存储在NSMutableArray
。
答案 0 :(得分:1)
试试这个家伙
-(NSMutableArray*)pullOutAllImagesInDocumentsDirectory{
NSArray *listOfPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [listOfPaths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *imageFilePathsCollection = [[NSMutableArray alloc] init];
for (int imageIndex=0; imageIndex<filePathsArray.count; imageIndex++) {
NSString *strFilePath = [filePathsArray objectAtIndex:0];
if ([[strFilePath pathExtension] isEqualToString:@"png"]||
[[strFilePath pathExtension] isEqualToString:@"PNG"]||
[[strFilePath pathExtension] isEqualToString:@"jpg"]) {
[imageFilePathsCollection addObject:[filePathsArray objectAtIndex:imageIndex]];
}
}
return imageFilePathsCollection;
}
注意:它会提供图片网址路径。为了获得图像,您可以使用
[yourImageView setImage:[UIImage imageWithContentsOfFile:"Path Of Image"]];
答案 1 :(得分:0)
我假设您的意思是特定于您的应用沙箱的文档目录。如果是这样,您可以使用[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
循环浏览该目录并查找具有.png或.jpg e.t.c的文件。路径扩展。
即:
NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSArray *array = [[NSFileManager defaultManager] contentsAtPath:string];
for (NSString *doc in array) {
NSLog(@"%@", doc);
if ([doc.pathExtension isEqualToString:@"jpg"]) {
// Is jpg image obviously you want to search for different formats also
}
}