未从数据加载到UICollectionView但从简单数组加载的图像数组?

时间:2018-02-02 23:11:01

标签: ios objective-c arrays uicollectionview

这是一个奇怪的,真的让我难过!所以我希望有人能提供帮助。

我在IB中设置的CollectionView可以使用以下数组正常工作:

_imageArray = [NSMutableArray arrayWithObjects:@"shop.JPG",@"shop1.jpg",@"shop2.jpg",@"Turkish_Tea.png", nil];

然而,当我将文档目录图像加载到数组中时,它们根本没有加载到CollectioView中?

这里是我从apps文件夹中获取图像到数组中的位置,NSLog显示它们在那里!

    NSString *extension = @"png";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];

    NSLog(@"files array - %@", directoryContent);
    NSMutableArray *fileNameArray = [NSMutableArray arrayWithCapacity:directoryContent.count];
    NSString *fileName;

    for (fileName in directoryContent) {
        if ([[fileName pathExtension] isEqualToString:extension]) {
            [fileNameArray addObject:fileName];
        }
    }

    NSLog(@"Images added to array are %@",fileNameArray);

    2018-02-02 23:09:23.326146+0000 Bodrum[579:117199] Images added to array are (
    "2018-02-02 20:59:06.png",
    "2018-02-02 19:58:23.png",
    "2018-02-02 19:58:08.png",
    "2018-02-02 22:16:27.png",
    "2018-02-02 22:17:55.png",
    "2018-02-02 22:16:12.png",
    "2018-02-02 19:57:45.png")


Am I missing something obvious here?

Thanks in advance



Whole code snippet as requested:

    - (void)viewDidAppear:(BOOL)animated {

    NSString *extension = @"png";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];

    NSLog(@"files array - %@", directoryContent);
    NSMutableArray *fileNameArray = [NSMutableArray arrayWithCapacity:directoryContent.count];
    NSString *fileName;

    for (fileName in directoryContent) {
        if ([[fileName pathExtension] isEqualToString:extension]) {
            [fileNameArray addObject:fileName];
        }
    }

    NSLog(@"Images added to array are %@",fileNameArray);

    _imageArray = [NSMutableArray arrayWithObjects:@"shop.JPG",@"shop1.jpg",@"shop2.jpg",@"Turkish_Tea.png", nil];//fileNameArray

    NSLog(@"Image Array is %@",_imageArray);

    NSLog(@"Image Array contains %lu",_imageArray.count);

    [self.collectionView reloadData];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
{

    return _imageArray.count;



}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];


    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];

    imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];

    return cell;

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
{
    return 1;
}

1 个答案:

答案 0 :(得分:0)

经过多次搜索和游戏,我改变了这一部分:

for (fileName in directoryContent) {
    if ([[fileName pathExtension] isEqualToString:extension]) {
        [fileNameArray addObject:fileName];
    }
}

以下内容:

for (fileName in contents) {

    if ([[fileName pathExtension] isEqualToString:extension])
    {

        [fileNameArray addObject:[UIImage imageWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:fileName ]]];
    }
}

我知道有一个包含我的UIImages的数组!