如何使用一个UICollectionView加载多个电影的拇指图像

时间:2016-05-31 10:09:11

标签: ios objective-c uicollectionview avfoundation

我正在构建一个媒体编辑项目,允许用户使用多种媒体进行编辑。为了满足这一需求,我在UICollectionView中推送UICollectionViewCell

内部UICollectionView加载电影的拇指图像。

示例代码:

- (void)setupUI
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
//    flowLayout.itemSize = CGSizeMake(100, 200);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionView = collectionView;
    collectionView.backgroundColor = [UIColor clearColor];
    [self addSubview:collectionView];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.scrollEnabled = NO;
    collectionView.userInteractionEnabled = NO;
//    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self);
//    }];

    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];


}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    PLog(@"count - %ld",self.videoItem.thumbnails.count);
    return self.thumbImages.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    PLog(@"kkkkkkkkkkk,cell item %ld,cell address %@",indexPath.item,cell);
    UIImage *image = self.thumbImages[indexPath.item];
    if ([image isKindOfClass:[NSNull class]]) {
        image = [UIImage imageWithColor:[UIColor blackColor]];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [cell addSubview:imageView];
//    imageView.userInteractionEnabled = YES;

    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(cell);
    }];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(_videoItem.thumbWidth, CollectionHeight);
}

来自多个不同电影的外部UICollectionView数据源,自定义UICollectionViewLayout

示例代码:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        UICollectionViewCell *cell = nil;
    PDVideoItemCollectionViewCell *videoCell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
                    PLog(@"indexrow - %ld",indexPath.item);
                    if (videoCell.collectionView == nil) {
                        [videoCell setupUI];
                    }

                    videoCell.videoItem = [self.timelineItems objectAtIndex:indexPath.item];
                    videoCell.videoItem.videoCell = videoCell;
                    [videoCell reloadCollectionData];

                    PLog(@"cellll - %@",videoCell);
                    cell = videoCell;
    cell.backgroundColor = MainBackgroundColor;
        return cell;
    }

调试控制台日志:

[PDVideoItem.m:172] image - <UIImage: 0x1268a3780>, {280, 158}--46
[PDVideoItem.m:172] image - <UIImage: 0x12689b800>, {280, 158}--47
[PDVideoItem.m:172] image - <UIImage: 0x12690a000>, {280, 158}--48
[PDVideoItem.m:172] image - <UIImage: 0x126821b60>, {280, 158}--49
[PDVideoItem.m:172] image - <UIImage: 0x1268a43d0>, {280, 158}--50
[PDVideoItem.m:184] done
[PDVideoItemCollectionViewCell.m:144]   count - 51
[PDVideoItemCollectionViewCell.m:151]   kkkkkkkkkkk,cell item 0,cell address <UICollectionViewCell: 0x12692de30; frame = (0 0; 112.5 63.2812); layer = <CALayer: 0x12691ff80>>
[PDVideoItemCollectionViewCell.m:151]   kkkkkkkkkkk,cell item 1,cell address <UICollectionViewCell: 0x126930930; frame = (112.5 0; 112.5 63.2812); layer = <CALayer: 0x12692f310>>
[PDVideoItemCollectionViewCell.m:151]   kkkkkkkkkkk,cell item 2,cell address <UICollectionViewCell: 0x1263aab60; frame = (225 0; 112.5 63.2812); layer = <CALayer: 0x12688f230>>
[PDVideoItemCollectionViewCell.m:151]   kkkkkkkkkkk,cell

enter image description here

情境描述:2电影= 2外部UIcollectionViewCell = 2内部UIcollectionView,总共82个拇指= 82个内部UIcollectionViewCell kkkkkkkkkkk登录collectionView: cellForItemAtIndexPath:方法,计数25

现在出现内部UIcollectionViewCell似乎没有ReUse的问题,当我使用UIPinchGestureRecognizer进行时间轴缩放时,内存将继续增加并且UI加载会很慢。内部UICollectionView的委托方法{{1}我在滚动动作时永远不会调用

呃,其实我觉得主要的问题是我第一次生成所有电影大拇指,但由于内部集合ViewCell不重用,我根据Item Indexpath无法生成拇指。

这个项目很多地引用了Apple的应用程序collectionView: cellForItemAtIndexPath:,也许我描述的应用情况不明确,只需下载iMovie

希望你的好主意。

1 个答案:

答案 0 :(得分:0)

首先,内部UICollectionView存在内存问题,即使您已将UIImageView子视图添加到视图的层次结构中,也会添加UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [cell addSubview:imageView]; 子视图:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        PLog(@"kkkkkkkkkkk,cell item %ld,cell address %@",indexPath.item,cell);
        UIImage *image = self.thumbImages[indexPath.item];
        if ([image isKindOfClass:[NSNull class]]) {
            image = [UIImage imageWithColor:[UIColor blackColor]];
        }
        UIImageView *imageView;
        if ([cell viewWithTag: ImageView_TAG]) {
               imageView = [[UIImageView alloc] initWithImage:image];
               imageView.tag = ImageView_TAG
               [cell addSubview:imageView];
        } else {
               imageView = [cell viewWithTag: ImageView_TAG]
               imageView.image = image
        }

    //    imageView.userInteractionEnabled = YES;

        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(cell);
        }];
        return cell;
    }

所以你必须用以下内容替换它:

UICollectionView

然后对于外部collectionView:cellForItemAtIndexPath,您必须将集合视图分配给videoCell.collectioView = collectioView 中的videoCell,如:

import numpy as np
a = ("Control", "Group1")
b = (("Control", "Group1"), ("Control", "Group1", "Group2))

这将节省您的记忆,并通过有效使用重用单元格来避免增加内存

祝你好运:)