gpt4 book ai didi

ios - 如何使用动态内容填充 Collection View

转载 作者:行者123 更新时间:2023-11-29 02:12:47 25 4
gpt4 key购买 nike

如果问题有点模糊,我深表歉意。

我遇到的问题是:我有一个包含 9 个单元格的 Collection View 。每个单元格都包含 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

任何帮助将不胜感激谢谢

最佳答案

您忘记在您的案例 7 中加入 break;。这就是单元格 7 和 8 显示相同数据的原因。

关于ios - 如何使用动态内容填充 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29092329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com