我想显示目录中的所有图片,但是我在目录中创建文件夹,以便我可以对图片进行排序。我想在几个文件夹中显示所有图片。我正在使用代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
allImagesArray = [[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *location=@"Bottoms"@"Top"@"Right"@"Left"@"Down"@"Up";
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
collectionTrash.delegate =self;
collectionTrash.dataSource=self;
for(NSString *str in directoryContent){
NSLog(@"i");
NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[allImagesArray addObject:image];
NSLog(@"array:%@",[allImagesArray description]);
}}}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
NSLog(@"j");
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [allImagesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseID = @"ReuseID";
TrashCell *mycell = (TrashCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
UIImageView *imageInCell = (UIImageView*)[mycell viewWithTag:1];
imageInCell.image = [allImagesArray objectAtIndex:indexPath.row];
NSLog(@"a");
return mycell;
}
如果你看到我的代码你可以注意到我已经把NSLOG i和j。 j出现,但我没有....我的方式错误显示几个文件夹中的所有图片?我没有任何错误。
答案 0 :(得分:0)
如果你有多个文件夹,那么你需要遍历文件夹然后文件夹内容来处理所有文件夹。
这一行:
NSString *location=@"Bottoms"@"Top"@"Right"@"Left"@"Down"@"Up";
技术上是合法的,我想你认为它会为你做一些数组/迭代的事情。它不会。它只是将所有字符串连接在一起。你可能想要更像的东西:
NSArray *locations = @[ @"Bottoms", @"Top", @"Right", @"Left", @"Down", @"Up" ];
然后你可以在文件夹上运行循环,然后运行内容:
for(NSString *folder in locations) {
// get the folder contents
for(NSString *file in directoryContent) {
// load the image
}
}