【问题标题】:Array of images not loading into UICollectionView from data but do from simple array?图像数组不是从数据加载到 UICollectionView 而是从简单数组加载?
【发布时间】:2018-02-02 23:11:00
【问题描述】:

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

我在 IB 中设置了一个 CollectionView,所有这些都可以使用以下数组:

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

但是,当我将文档目录图像加载到数组中时,它们根本不会加载到 CollectioView 中?

这是我从应用程序文件夹中获取我的图像到数组中的地方,并且 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;
}

【问题讨论】:

  • 您需要为您的收藏视图显示相关代码。
  • “但是,当我将文档目录图像加载到数组中时,它们根本没有加载到 CollectioView 中?”阙?只要看到您的代码,就没有集合视图对象的迹象。
  • 您能否尝试将您的图像名称重命名为没有所有这些冒号和空格的名称。尝试使用名称为 image1.png 的图像,看看它是否加载。
  • 这里是加载视图中的图像的整个代码确实加载和收集视图。 (正如您将看到的,我目前正在我的包中使用一个简单的图像数组进行测试,但 //fileNameArray 是我在 _imageArray = fileNameArray 中尝试的?
  • 嗨@MilanGupta,感谢您的建议,我已将图像名称的格式更改如下:2018-02-03 10:36:50.345919+0000 Bodrum[794:213929] Image Array是(“20180203103649.png”、“20180203103556.png”、“20180203103536.png”)还是不行?!

标签: ios objective-c arrays uicollectionview


【解决方案1】:

经过大量搜索和玩耍后,我更改了此部分:

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 的数组!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2013-08-27
    • 2011-08-02
    • 2021-10-16
    • 2016-10-13
    相关资源
    最近更新 更多