没有CollectionView目录中的图片

我想显示我的目录中的所有图片,但是我正在创build目录中的文件夹,以便我可以对图片进行sorting。 我想在几个文件夹中显示所有的图片。 我正在使用的代码

- (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我和j。 j来了,但我不….我的方式是错误的显示在几个文件夹中的所有图片? 我没有任何错误。

如果您有多个文件夹,则需要迭代文件夹,然后遍历文件夹内容以处理所有文件夹。

虽然这一行:

 NSString *location=@"Bottoms"@"Top"@"Right"@"Left"@"Down"@"Up"; 

在技​​术上是合法的,我猜你认为它会为你做一些数组/迭代的事情。 它不会。 它只是连接所有的string。 你可能想要更类似于:

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