如何使用动态内容填充集合视图

时间:2015-03-17 06:04:55

标签: ios uicollectionview

如果问题有点含糊,我道歉。

我遇到的问题是: 我有一个包含9个单元格的集合视图。每个单元格都包含UIImage的过滤版本和过滤器的名称。由于某种原因,索引7(代码中的情况7)没有被使用,看起来索引8(情况8)显示两次。

以下是相关方法:

-(UIImage *)filteredImageFromImage:(UIImage *)image withFilterIndex:(NSUInteger)filterIndex{

    CIImage *beginImage =
    [CIImage imageWithCGImage:image.CGImage];

    // 1
    CIContext *context = [CIContext contextWithOptions:nil];

    CIFilter *filter;

    switch (filterIndex) {
        case 0:
            filter = [CIFilter filterWithName:@"CIPhotoEffectNoir" keysAndValues: kCIInputImageKey, beginImage, nil];
            break;
        case 1:
            filter = [CIFilter filterWithName:@"CIPhotoEffectMono" keysAndValues: kCIInputImageKey, beginImage, nil];
            break;
        case 2:
            filter = [CIFilter filterWithName:@"CIPhotoEffectTonal" keysAndValues: kCIInputImageKey, beginImage, nil];
            break;
        case 3:
            filter = nil;
            break;
        case 4:
            filter = [CIFilter filterWithName:@"CIPhotoEffectFade" keysAndValues: kCIInputImageKey, beginImage, nil];
            break;
        case 5:
            filter = [CIFilter filterWithName:@"CIPhotoEffectChrome" keysAndValues: kCIInputImageKey, beginImage, nil];
            break;
        case 6:
            filter = [CIFilter filterWithName:@"CIPhotoEffectProcess" keysAndValues: kCIInputImageKey, beginImage, nil];
            break;
        case 7:
            filter = [CIFilter filterWithName:@"CIPhotoEffectTransfer" keysAndValues: kCIInputImageKey, beginImage, nil];
        case 8:
            filter = [CIFilter filterWithName:@"CIPhotoEffectInstant" keysAndValues: kCIInputImageKey, beginImage, nil];
            break;
        default:
            filter = nil;
            break;
    }

    CIImage *outputImage;

    if (!filter) {
        outputImage = beginImage;
    }else{
        outputImage = [filter outputImage];
    }
    // 2
    CGImageRef cgimg =
    [context createCGImage:outputImage fromRect:[beginImage extent]];

    // 3
    UIImage *newImage = [UIImage imageWithCGImage:cgimg ];


    // 4
    CGImageRelease(cgimg);
    return newImage;

}

-(NSString *)effectNameforIndex:(NSInteger )index{

    NSString *name;

    switch (index) {
        case 0:
            name = @"noir";
            break;
        case 1:
            name = @"mono";
            break;
        case 2:
            name = @"tonal";
            break;
        case 3:
            name = @"original";
            break;
        case 4:
            name = @"fade";
            break;
        case 5:
            name = @"chrome";
            break;
        case 6:
            name = @"process";
            break;
        case 7:
            name = @"transfer";
        case 8:
            name = @"instant";
            break;
        default:
            name = @"original";
            break;
    }

    return name;


}


#pragma mark - UICollectionViewDataSource

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

    return 9;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FilterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"filterPreviewCell" forIndexPath:indexPath];
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.synchronous = YES;
    [[PHImageManager defaultManager] requestImageForAsset:self.firstAsset targetSize:cell.imageView.frame.size contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *result, NSDictionary *info) {
        if (result) {
            cell.imageView.image = [self filteredImageFromImage:result withFilterIndex:indexPath.row];
            cell.nameLabel.text = [self effectNameforIndex:indexPath.row];
        }
    }];

    cell.layer.cornerRadius = 10.0;
    cell.layer.borderColor = [[UIColor colorWithWhite:1.0 alpha:0.72]CGColor];
    cell.layer.borderWidth = 2.0;
    cell.clipsToBounds = YES;

    return cell;
}

这是结果的图像:

enter image description here

非常感谢任何帮助 感谢

1 个答案:

答案 0 :(得分:1)

您忘记在案例中添加break;。这就是为什么单元格7和8都显示相同的数据。