没有 CollectionView 目录中的图片 [英] Don't have the pictures from directory on CollectionView
问题描述
我想显示我的目录中的所有图片,但是我正在目录中创建文件夹,以便我可以对图片进行排序.我想显示几个文件夹中的所有图片.我正在使用代码
I would want to show all the pictures in my directory however I am creating Folders in the Directory so that I can sort the pictures. I want to show all of the pictures in several folders. I am using the code
- (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 出现但 i 没有出现......我显示几个文件夹中的所有图片的方式是否错误?我没有任何错误.
If you see my code you can notice that I have put NSLOG i and j. The j comes up but the i does not.... Is my way wrong showing all the pictures that are in several folders? I do not have any error.
推荐答案
如果您有多个文件夹,则需要遍历文件夹,然后遍历文件夹内容以处理所有文件夹.
If you have multiple folders then you need to iterate over the folders and then the folder contents to process all of it.
虽然这一行:
NSString *location=@"Bottoms"@"Top"@"Right"@"Left"@"Down"@"Up";
在技术上是合法的,我猜你认为它会为你做一些数组/迭代的事情.它不会.它只是将所有字符串连接在一起.你可能想要更多类似的东西:
Is technically legal, I guess you're thinking it will do some array / iteration thing for you. It won't. It just concatenates all of the strings together. You probably want something more like:
NSArray *locations = @[ @"Bottoms", @"Top", @"Right", @"Left", @"Down", @"Up" ];
然后你可以在文件夹和内容上运行一个循环:
Then you can run a loop over the folders and then the contents:
for(NSString *folder in locations) {
// get the folder contents
for(NSString *file in directoryContent) {
// load the image
}
}
这篇关于没有 CollectionView 目录中的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!