在相册中下载的CollectionView图像对于所有CollectionViewCells

时间:2016-05-18 10:02:54

标签: ios objective-c uicollectionview uicollectionviewcell

当我通过互联网下载相册时,它会创建一些带有封面图像的文件夹。当我想在collectionView中显示该图像时,它始终显示所有相册的相同图像。例如,如果我下载专辑1,专辑2,专辑3分别用ablum 1中的cover.png,ablum2中的cover.png,专辑3中的cover.png。

以下是代码:(我确切地说,对于测试,如果我使用的是来自数组中带有图像的图像,那就可以了。)

#pragma mark - Collection View
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[DBManagement database] getNBDownloadedAlbums: [[NSUserDefaults standardUserDefaults] objectForKey:@"SettingsWebview"]];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";

    AlbumsController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!cell)
    {
        cell = [[AlbumsController alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width , cell.frame.size.height)];
    }

    NSString * coverImagePath = [[[[albums objectAtIndex:indexPath.row] objectForKey:@"URL"] stringByDeletingLastPathComponent] stringByAppendingString:@"/cover.png"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:coverImagePath])
    {
        NSLog(@"Cover : perso");
        cell.imageView.image = [UIImage imageNamed:coverImagePath];
        cell.label.text = coverImagePath;
    }
    else
    {
        NSLog(@"Cover : generique");
        cell.imageView.image = [UIImage imageNamed:@"nocover.png"];
        cell.label.text = @"nocover.png";
    }

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(200, 155);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

修改 当我一步一步地做,似乎线路没有完成这项工作:

cell.imageView.image = [UIImage imageNamed:coverImagePath];

不会加载好图像。我不明白,因为当我阅读coverImagePath时,名称是好的,封面是好的图像,因为我直接在ipad上检查iexplorer,但图像加载到imageView.image是不好的。它始终是第一个在每个单元格上重复的图像,即使名称正确也是如此。奇怪。

第二次编辑: 对于您的信息,图像封面的名称在每个文件夹cover.png中是相同的,这是问题吗?

1 个答案:

答案 0 :(得分:0)

第一次修正:我为封面图片使用了不同的名称,现在可以使用了。我认为当显示来自不同文件夹但具有相同名称的几个单元格图像时,Xcode和Objective C不能很好地管理(例如cover.png。而不是,我把cover_album1.png,cover_album2.png等... )。

第二次更正:我确切地说我必须添加以下代码来更新集合视图的图像:

CollectionViewCell.m中的

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        self.imageView = [UIImageView new];
    }
    return self;
}

我确切地说,如果你将storyView与故事板一起使用,你必须使用 initWithCoder 而不是 initWithFrame